从0开始python学习-33.夹具@pytest.fixture(scope=““,params=““,autouse=““,ids=““,name=““)

目录

[1. 创建夹具](#1. 创建夹具)

[1.1 pytest方式](#1.1 pytest方式)

[1.2 unittest方式](#1.2 unittest方式)

[2. 使用夹具](#2. 使用夹具)

[2.1 通过参数引用](#2.1 通过参数引用)

[2.2 通过函数引用](#2.2 通过函数引用)

[3. 参数详解](#3. 参数详解)

[3.1 scope:作用域](#3.1 scope:作用域)

[3.2 params-参数化](#3.2 params-参数化)

[3.3 autouse=True表示自动使用,默认为False](#3.3 autouse=True表示自动使用,默认为False)

[3.4 ids:设置变量名](#3.4 ids:设置变量名)

[3.5 name:别名](#3.5 name:别名)


1. 创建夹具

1.1 pytest方式

python 复制代码
@pytest.fixture()
def test_a():
    print('case执行之前执行')
    yield
    print('case执行之后执行')

1.2 unittest方式

python 复制代码
class Test:
    def setup_method(self):
        print('setup_method:case执行之前执行--用例之前执行一次')
    def teardown_method(self):
        print('teardown_method:case执行之后执行--每个case执行之前均执行')
    def setup_class(self):
        print('setup_class:case执行之前执行--每个case执行之后均执行')
    def teardown_class(self):
        print('teardown_class:case执行之后执行--全部用例执行完了之后才执行')

2. 使用夹具

2.1 通过参数引用

2.2 通过函数引用

3. 参数详解

@pytest.fixture(scope="",params="",autouse="",ids="",name="")

3.1 scope:作用域

表示标记方法的作用域:function(默认),class,module,package,session

session > module > class > function

function:每个用例都有一个

python 复制代码
@pytest.fixture(scope='function')
def test_a():
    print('之前执行')
    yield
    print('之后执行')
class Test_A:
    def test_1(self,test_a):
        print('test_1')
    def test_2(self,test_a):
        print('test_2')
class Test_B:
    def test_3(self,test_a):
        print('test_3')

class:每个class 共用一个

python 复制代码
@pytest.fixture(scope='class')
def test_a():
    print('之前执行')
    yield
    print('之后执行')
class Test_A:
    def test_1(self,test_a):
        print('test_1')
    def test_2(self):
        print('test_2')
class Test_B:
    def test_3(self):
        print('test_3')

下面就不举例了:module:每个文件共用一个;package:每个包共用一个;session:全局共用一个

3.2 params-参数化

支持列表\[\]、元组()、字典列表{},{},字典元组({},{})

fixture引用外部参数

python 复制代码
param = ['111',[1,2],('a','b')]
@pytest.fixture(params=param)
def test_a(request):
    test = request.param
    return test

def test_1(test_a):
    print(test_a)

fixture标签直接进行参数化

python 复制代码
@pytest.fixture(params=[1,2,'aaaa'])
def test_b(request):
    test1 = request.param
    return test1
def test_2(test_b):
    print(test_b)

3.3 autouse=True表示自动使用,默认为False

autouse=True时无论是否使用都会被使用

python 复制代码
@pytest.fixture(autouse=True)
def test_a():
    print('test_a')

def test_1():
    print('test_1')

autouse=False时必须要手动调用了才会被使用

python 复制代码
@pytest.fixture(autouse=False)
def test_b():
    print('test_b')

def test_2(test_b):
    print('test_2')

3.4 ids:设置变量名

当使用params参数化时,给每一个值设置一个变量名

python 复制代码
param = ['111',[1,2],('a','b')]
@pytest.fixture(params=param,ids=['user1','user2','user3'])
def test_a(request):
    test = request.param
    return test

def test_1(test_a):
    print(test_a)

3.5 name:别名

表示被@pytest.fixture标记的方法取一个别名,当取了别名后,原来的名称就不能用了

python 复制代码
@pytest.fixture(name='yyyy')
def test_a():
    print('11111')

def test_1(yyyy):
    print('test_1')
def test_2(test_a):
    print('test_1')
相关推荐
卷无止境42 分钟前
独立开发者的"富矿地带":哪些垂直领域值得你押注一辈子?
后端·python
Java后端的Ai之路1 小时前
Python进阶探索29_eval内置函数
开发语言·python·探索·eval·内置函数
计算机毕业编程指导师1 小时前
计算机毕设选题推荐:基于Hadoop与Spark的Steam游戏数据分析系统源码 毕业设计 选题推荐 毕设选题 数据分析 机器学习 深度学习
大数据·hadoop·python·计算机·spark·毕业设计·steam游戏
计算机毕业编程指导师1 小时前
【计算机毕设选题推荐】基于Hadoop+Spark的白鹿抖音评论大数据分析与可视化系统源码 毕业设计 选题推荐 毕设选题 数据分析 机器学习 深度学习
大数据·hadoop·python·计算机·spark·毕业设计·抖音评论
Zootopia6261 小时前
飞行力学知识梳理1|飞行性能与稳定性
人工智能·python·算法·机器学习·无人机·学习方法·信息与通信
Y3815326621 小时前
搜索 API 延迟优化:并发数、超时与超时的真实代价
python·搜索引擎
念越1 小时前
Allure 测试报告保姆级教程:从安装到 pytest 集成与报告生成
pytest·allure
wangqiaowq2 小时前
PII 脱敏指的是:把个人身份信息(PII)中能识别到具体个人的敏感部分,用替换、遮蔽、变形等方式处理掉,使得数据在保留可用性的同时,不再直接暴露个人身份。
python
风早爽太2 小时前
Python 学习笔记:SQLModel 使用外键关联查询数据
python·fastapi·sqlmodel
专业程序开发源2 小时前
springboot社区养老系统44071-计算机课程设计、毕业设计
vue.js·spring boot·后端·python·django·php·课程设计