zoukankan      html  css  js  c++  java
  • 【pytest学习8】pytest的autouse参数

    需要传入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'])
    复制代码

    声明 欢迎转载,但请保留文章原始出处:) 博客园:https://www.cnblogs.com/chenxiaomeng/ 如出现转载未声明 将追究法律责任~谢谢合作
  • 相关阅读:
    MapReduce教程(一)基于MapReduce框架开发<转>
    postgresql with递归
    mysql中递归树状结构<转>
    java获取上周任意一天的日期
    IBatis批量插入数据
    UI控件篇——UIPageControl及其自定义
    Android 3.0开始引入fragments(碎片、片段)类
    APACHE LOG4J™ 2
    java 反射
    PreparedStatement用途
  • 原文地址:https://www.cnblogs.com/chenxiaomeng/p/14817992.html
Copyright © 2011-2022 走看看