zoukankan      html  css  js  c++  java
  • pytest---fixture中autouse参数

    前言

      在调用fixture的时候,需要传入fixture的名称,如果用例都需要这个fixture,每个用例都进行传入fixture名称,这就比较麻烦了,有什么好的方法?当然fixture中的autouse就是控制传参范围。

    autouse

    autouse属于fixture参数中的其中一个,默认是为False,不会使作用域的方法全部都进行使用,当设置为True时,作用域范围内的用例都会进行执行fixture内容。

    import pytest
    
    @pytest.fixture(autouse=True)
    def login():
        print('完成登录')
        yield
        print('退出登录')
    
    
    class Test_01:
        def test_01(self):
            print('---用例01---')
    
        def test_02(self):
            print('---用例02---')
    
    
    if __name__ == '__main__':
        pytest.main(['-vs'])

    通过执行结果表明:当我们设置autouse参数为True时,默认测试作用域fixture内的测试函数都会全部执行。

    混合使用

    其实autouse设置为True和其他的fixture进行使用过程中是互不影响的,可以一起使用。

    import pytest
    
    
    @pytest.fixture(autouse=True)
    def ti():
        print('每个测试用例都会执行!')
    
    
    @pytest.fixture()
    def login():
        print('
    完成登录')
        yield
        print('
    退出登录')
    
    
    class Test_01:
        def test_01(self,login):
            print('---用例01---')
    
        def test_02(self):
            print('---用例02---')
    
    
    if __name__ == '__main__':
        pytest.main(['-vs'])

    简答的介绍了autouse的参数内容,小伙伴们可以自己动手进行操作下。

  • 相关阅读:
    USTC 软硕讯飞班参考资源
    nginx 负载均衡
    Meidawiki 配置
    10 款实用的jquery插件
    Mongodb 定时释放内存
    互联网产品精神解读
    简单的缓冲区溢出实验
    fatal error C1902: 程序数据库管理器不匹配;请检查安装解决
    C#的override、new、vitutal一例
    SQL Server 2008导入、导出数据库
  • 原文地址:https://www.cnblogs.com/qican/p/14234772.html
Copyright © 2011-2022 走看看