zoukankan      html  css  js  c++  java
  • pytest -fixture的3种用法(autouse=True)

    fixture里面有个参数autouse,默认是Fasle没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了

    调用fixture三种方法

    • 1.函数或类里面方法直接传fixture的函数参数名称
    • 2.使用装饰器@pytest.mark.usefixtures()修饰
    • 3.autouse=True自动使用   

    用例传fixture参数

    方法一:先定义start功能,用例全部传start参数,调用该功能

    # content of test_06.py
    import time
    import pytest
    
     
    
    @pytest.fixture(scope="function")
    def start(request):
        print('
    -----开始执行function----')
    
    
    def test_a(start):
        print("-------用例a执行-------")
    
    
    
    
    class Test_aaa():
    
        def test_01(self, start):
            print('-----------用例01--------------')
    
        def test_02(self, start):
            print('-----------用例02------------')
    
    if __name__ == "__main__":
        pytest.main(["-s", "test_06.py"])

    装饰器usefixtures

    方法二:使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

    # content of test_07.py
    import time
    import pytest
    
     
    
    @pytest.fixture(scope="function")
    def start(request):
        print('
    -----开始执行function----')
    
    
    @pytest.mark.usefixtures("start")
    def test_a():
        print("-------用例a执行-------")
    
    @pytest.mark.usefixtures("start")
    class Test_aaa():
    
        def test_01(self):
            print('-----------用例01--------------')
    
        def test_02(self):
            print('-----------用例02------------')
    
    if __name__ == "__main__":
        pytest.main(["-s", "test_07.py"])

    设置autouse=True

    方法三、autouse设置为True,自动调用fixture功能

    • start设置scope为module级别,在当前.py用例模块只执行一次,autouse=True自动使用
    • open_home设置scope为function级别,每个用例前都调用一次,自动使用
    # content of test_08.py
    import time
    import pytest
     
    
    @pytest.fixture(scope="module", autouse=True)
    def start(request):
        print('
    -----开始执行moule----')
        print('module      : %s' % request.module.__name__)
        print('----------启动浏览器---------')
        yield
        print("------------结束测试 end!-----------")
    
    
    
    @pytest.fixture(scope="function", autouse=True)
    def open_home(request):
        print("function:%s 
    --------回到首页--------" % request.function.__name__)
    
    
    def test_01():
        print('-----------用例01--------------')
    
    def test_02():
        print('-----------用例02------------')
    
    if __name__ == "__main__":
        pytest.main(["-s", "test_08.py"])

    运行结果:

    ============================= test session starts =============================
    platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
    rootdir: D:\, inifile:
    plugins: metadata-1.7.0, html-1.19.0, allure-adaptor-1.7.10
    collected 2 items
    
    ............YOYOpeizhi	est_08.py 
    -----开始执行moule----
    module      : YOYO.peizhi.test_08
    ----------启动浏览器---------
    function:test_01 
    --------回到首页--------
    -----------用例01--------------
    .function:test_02 
    --------回到首页--------
    -----------用例02------------
    .------------结束测试-----------
    
    
    ========================== 2 passed in 0.01 seconds ===========================
    

    上面是函数去实现用例,写的class里也是一样可以的

    # content of test_09.py
    import time
    import pytest
     
    
    @pytest.fixture(scope="module", autouse=True)
    def start(request):
        print('
    -----开始执行moule----')
        print('module      : %s' % request.module.__name__)
        print('----------启动浏览器---------')
        yield
        print("------------结束测试 end!-----------")
    
    
    
    class Test_aaa():
        @pytest.fixture(scope="function", autouse=True)
        def open_home(self, request):
            print("function:%s 
    --------回到首页--------" % request.function.__name__)
    
    
        def test_01(self):
            print('-----------用例01--------------')
    
        def test_02(self):
            print('-----------用例02------------')
    
    if __name__ == "__main__":
        pytest.main(["-s", "test_09.py"])
  • 相关阅读:
    2017年5月19日13:58:17 问题记录
    2017年5月17日20:14:29 rabbitmq 消费 异常信息无法处理 导致轮询
    2017年5月12日15:10:46 rabbitmq不支持非阻塞调用服务器
    2017年5月11日17:43:06 rabbitmq 消费者队列
    2017年5月10日16:11:28 定义所有问题
    MyBatis Plus 只插入只有自增id字段的表
    Centos 7 关闭报警音
    关于git项目切换远程提交路径和提交仓库
    IDEA通过git回滚到某个提交节点或某个版本
    IDEA使用@Autowired注解报错解决方案
  • 原文地址:https://www.cnblogs.com/jodie2019/p/13205370.html
Copyright © 2011-2022 走看看