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


    目录

    1. 创建夹具

    1.1 pytest方式

    1.2 unittest方式

    2. 使用夹具

    2.1 通过参数引用

    2.2 通过函数引用

    3. 参数详解

    3.1 scope:作用域

    3.2 params-参数化

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

    3.4 ids:设置变量名

     3.5 name:别名


    1. 创建夹具

    1.1 pytest方式

    1. @pytest.fixture()
    2. def test_a():
    3. print('case执行之前执行')
    4. yield
    5. print('case执行之后执行')

    1.2 unittest方式

    1. class Test:
    2. def setup_method(self):
    3. print('setup_method:case执行之前执行--用例之前执行一次')
    4. def teardown_method(self):
    5. print('teardown_method:case执行之后执行--每个case执行之前均执行')
    6. def setup_class(self):
    7. print('setup_class:case执行之前执行--每个case执行之后均执行')
    8. def teardown_class(self):
    9. 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:每个用例都有一个

    1. @pytest.fixture(scope='function')
    2. def test_a():
    3. print('之前执行')
    4. yield
    5. print('之后执行')
    6. class Test_A:
    7. def test_1(self,test_a):
    8. print('test_1')
    9. def test_2(self,test_a):
    10. print('test_2')
    11. class Test_B:
    12. def test_3(self,test_a):
    13. print('test_3')

    class:每个class 共用一个

    1. @pytest.fixture(scope='class')
    2. def test_a():
    3. print('之前执行')
    4. yield
    5. print('之后执行')
    6. class Test_A:
    7. def test_1(self,test_a):
    8. print('test_1')
    9. def test_2(self):
    10. print('test_2')
    11. class Test_B:
    12. def test_3(self):
    13. print('test_3')

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

    3.2 params-参数化

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

    fixture引用外部参数

    1. param = ['111',[1,2],('a','b')]
    2. @pytest.fixture(params=param)
    3. def test_a(request):
    4. test = request.param
    5. return test
    6. def test_1(test_a):
    7. print(test_a)

    fixture标签直接进行参数化

    1. @pytest.fixture(params=[1,2,'aaaa'])
    2. def test_b(request):
    3. test1 = request.param
    4. return test1
    5. def test_2(test_b):
    6. print(test_b)

     

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

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

    1. @pytest.fixture(autouse=True)
    2. def test_a():
    3. print('test_a')
    4. def test_1():
    5. print('test_1')

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

    1. @pytest.fixture(autouse=False)
    2. def test_b():
    3. print('test_b')
    4. def test_2(test_b):
    5. print('test_2')

    3.4 ids:设置变量名

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

    1. param = ['111',[1,2],('a','b')]
    2. @pytest.fixture(params=param,ids=['user1','user2','user3'])
    3. def test_a(request):
    4. test = request.param
    5. return test
    6. def test_1(test_a):
    7. print(test_a)

     3.5 name:别名

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

    1. @pytest.fixture(name='yyyy')
    2. def test_a():
    3. print('11111')
    4. def test_1(yyyy):
    5. print('test_1')
    6. def test_2(test_a):
    7. print('test_1')

  • 相关阅读:
    Java面试笔试题大全
    基于OpenCV的轮廓检测(2)
    JavaScript高级复习下(60th)
    Js逆向教程-04浏览器调试工具-Application页面
    [附源码]JAVA毕业设计互联网保险网站(系统+LW)
    贪心算法,删数问题
    Python科学计算库练习题
    软件定制开发的细节|网站搭建|APP小程序定制
    Spring Security 在登录时如何添加图形验证码
    载誉前行 | 求臻医学MRD检测方案荣获金如意奖·卓越奖
  • 原文地址:https://blog.csdn.net/Meseiter/article/details/134366617