name: name参数表示可以对fixture的名称进行重命名:

注意:通过name重命名后,继续使用以前的名字调用会报错。

import pytest

@pytest.fixture(name='anjing')
def login():
print('\n登录操作')
yield
print('\n退出登录!') class TestLogin:
def test_01(self, anjing):
print('---用例01---') def test_02(self):
print('---用例02---') if __name__ == '__main__':
pytest.main(['-vs'])

  

params:参数化

  params表示fixture的参数化功能。这里会有一个request参数,主要用来接收fixture的返回结果。并通过request.param返回结果内容。

假设现在有一批 API 需要测试对不同数据库的支持情况(对所有数据库进行相同操作),最简单的方法就是针对每个数据库编写一个测试用例,但这包含大量重复代码,如数据库的连接、关闭,查询等。

使用固件抽离出数据库的通用操作,每个 API 都能复用这些数据库固件,同时可维护性也得到提升。

import pytest

@pytest.fixture(params=[
('redis', '6379'),
('elasticsearch', '9200')
])
def param(request):
print(f"数据库的配置:{request.param}")
return request.param @pytest.fixture(autouse=True)
def db(param):
print(f'数据库连接了{param}')
yield
print(f'数据库关闭了{param}') def test_api():
assert 1 == 1

  

执行结果

ids
  ids表示在fixture对参数化的内容进行加上标识,比如让别人知道这个传入的参数是什么意思。用于什么样的测试用例。默认是传参数内容:
 
import pytest

data = ['anjing', 'test', 'admin']

@pytest.fixture(params=data, ids=['user=anjing', 'user=test', 'user=admin'])
def login(request):
print('登录功能')
yield request.param
print('退出登录') class TestCase:
def test_01(self, login):
print('---用例01---')
print(f'登录的用户名{login}') def test_02(self):
print('---用例02---') if __name__ == '__main__':
pytest.main(['-vs'])

  

  

Pytest Fixture(三)的更多相关文章

  1. Pytest测试框架(三):pytest fixture 用法

    xUnit style 结构的 fixture用于初始化测试函数, pytest fixture是对传统的 xUnit 架构的setup/teardown功能的改进.pytest fixture为测试 ...

  2. Pytest fixture及conftest详解

    前言 fixture是在测试函数运行前后,由pytest执行的外壳函数.fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集.配置测试前系统的初始状态.为批量测试提供数据源等 ...

  3. pytest.fixture和普通函数调用

    普通函数嗲用def one(): a="aaaaaaaaaaa" return a def test_one(): s=one() print (s) test_one() pyt ...

  4. pytest 用 @pytest.mark.usefixtures("fixtureName")或@pytest.fixture(scope="function", autouse=True)装饰,实现类似setup和TearDown的功能

    conftest.py import pytest @pytest.fixture(scope="class") def class_auto(): print("&qu ...

  5. pytest fixture 利用 params参数实现用例集合

    @pytest.fixture有一个params参数,接受一个列表,列表中每个数据都可以作为用例的输入.也就说有多少数据,就会形成多少用例.如下面例子,就形成3条用例 test_parametrizi ...

  6. pytest fixture中scope试验,包含function、module、class、session、package

    上图是试验的目录结构 conftest.py:存放pytest fixture的文件 import uuid import pytest @pytest.fixture(scope="mod ...

  7. python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)

    pytest的setup和teardown函数(曾被一家云计算面试官问到过). pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.与python自带的unitest ...

  8. 学习-Pytest(三)setup/teardown

    1. 用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(setup_function/teardown_function)只对函数用例生效 ...

  9. pytest封神之路第三步 精通fixture

    首先放一句"狠话". 如果你不会fixture,那么你最好别说自己会pytest. (只是为了烘托主题哈,手上的砖头可以放下了,手动滑稽) fixture是什么 看看源码 def ...

  10. 『德不孤』Pytest框架 — 13、Pytest中Fixture装饰器(三)

    目录 9.ids参数说明 10.name参数说明 11.scope参数说明 (1)scope="function" (2)scope="class" (3)sc ...

随机推荐

  1. git 删除本地分支以及远程分支

    1.git branch -a 2. git branch -d v1.3(要删除的本地分支) 3.git push origin --delete v1.3(远程分支)

  2. c-->extern关键字的使用

    extern关键字的使用 作用:声明外部符号(使用其他文件的全局变量) 例子: 源文件: test.c #include <stdio.h> extern int year;//使用ext ...

  3. git+jenkins+ansible+gitlab部署网站

  4. sequlizejs学习笔记整理

    1.事务 try { const transaction = await sequelize.transaction(); const user = await User.findOne(..., { ...

  5. [Swift]Xcode格式化代码快捷键

    写代码最常用的快捷键莫过于格式化代码了,最近玩Swift,百度上没有找到简单明了的内容,特意记录一下. Control + I     自动调整缩进 可以选中相应的代码 或 Command + A全选 ...

  6. ZIAO日报 202302

    2023.2 2023年2月14日 10:23 2023.2.14 继续读<Multi-View Transformer for 3D Visual Grounding>,读到了relat ...

  7. Go--解析yaml文件

    yaml 文件是目前最常用的配置文件,使用go语言编写代码和工具时,也会用到yaml文件,将服务配置及中间件等信息定义到yaml文件中,后续可根据实际场景来选用. //先下载外部包 go get -u ...

  8. 安防视频监控系统前端摄像机——DSP与SOC摄像机

    一.DSP摄像机 DSP(Digital Signal Processing)即数字信号处理,它是利用数字计算机或专用数字信号处理设备,以数值计算的方法对信号进行采集.变换.综合.估值.识别等加工处理 ...

  9. linux express配置ssl证书,开启https访问

    1.下载ssl证书的Nginx版本,在项目根目录新建https空文件夹,把crt文件和key文件复制到https文件夹中; 2.安装依赖:npm i fs http https 3.修改www文件的内 ...

  10. vue element ui table 自动无限滚动组件

    <template>   <el-table     class="tableList"     ref="rw_table"     hei ...