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的参数内容,小伙伴们可以自己动手进行操作下。

  • 相关阅读:
    lostash 正则
    Mysql 利用multiline 实现多行匹配
    java中byte, int的转换
    mysql perl 抓取update语句
    $/ 改变换行符
    mysql 匹配update
    perl binlog dml操作报告
    mysql 分区 按 PARTITION BY RANGE (TO_DAYS(startTime))
    Mysql explain 查看分区表
    写作的力道——北漂18年(番外篇一)
  • 原文地址:https://www.cnblogs.com/qican/p/14234772.html
Copyright © 2011-2022 走看看