pytest-fixture的使用

 前置条件:
        1.文件路径:
            - Test_App
            - - test_abc.py
            - - pytest.ini
        2.pyetst.ini配置文件内容:
            [pytest]
            # 命令行参数
            addopts = -s
            # 搜索文件名
            python_files = test_*.py
            # 搜索的类名
            python_classes = Test_*
            # 搜索的函数名
            python_functions = test_*
  • 1.pytest之fixture
    fixture修饰器来标记固定的工厂函数,在其他函数,模块,类或整个工程调用它时会被激活并优先执行,
        通常会被用于完成预置处理和重复操作。
    方法:fixture(scope="function", params=None, autouse=False, ids=None, name=None)
    常用参数:
        scope:被标记方法的作用域
            function" (default):作用于每个测试方法,每个test都运行一次
            "class":作用于整个类,每个class的所有test只运行一次
            "module":作用于整个模块,每个module的所有test只运行一次
            "session:作用于整个session(慎用),每个session只运行一次
        params:(list类型)提供参数数据,供调用标记方法的函数使用
        autouse:是否自动运行,默认为False不运行,设置为True自动运行
  • 2.fixture第一个例子(通过参数引用)
    示例:
        import pytest
        class Test_ABC:
            @pytest.fixture()
            def before(self):
                print("------->before")
            def test_a(self,before): # ⚠️ test_a方法传入了被fixture标识的函数,已变量的形式
                print("------->test_a")
                assert 1
        if __name__ == '__main__':
            pytest.main("-s  test_abc.py")
    执行结果:
        test_abc.py 
        ------->before # 发现before会优先于测试函数运行
        ------->test_a
        .
  • 3.fixture第二个例子(通过函数引用)
      示例:
          import pytest
          @pytest.fixture() # fixture标记的函数可以应用于测试类外部
          def before():
              print("------->before")
          @pytest.mark.usefixtures("before")
          class Test_ABC:
              def setup(self):
                  print("------->setup")
              def test_a(self):
                  print("------->test_a")
                  assert 1
          if __name__ == '__main__':
              pytest.main("-s  test_abc.py")
    
      执行结果:
          test_abc.py 
          ------->before # 发现before会优先于测试类运行
          ------->setup
          ------->test_a
          .
    
  • 4.fixture第三个例子(默认设置为运行)
    示例:
        import pytest
        @pytest.fixture(autouse=True) # 设置为默认运行
        def before():
            print("------->before")
        class Test_ABC:
            def setup(self):
                print("------->setup")
            def test_a(self):
                print("------->test_a")
                assert 1
        if __name__ == '__main__':
            pytest.main("-s  test_abc.py")
    执行结果:
        test_abc.py 
        ------->before # 发现before自动优先于测试类运行
        ------->setup
        ------->test_a
        .
  • 5.fixture第四个例子(设置作用域为function)
    示例:
        import pytest
        @pytest.fixture(scope='function',autouse=True) # 作用域设置为function,自动运行
        def before():
            print("------->before")
        class Test_ABC:
            def setup(self):
                print("------->setup")
            def test_a(self):
                print("------->test_a")
                assert 1
            def test_b(self):
                print("------->test_b")
                assert 1
        if __name__ == '__main__':
            pytest.main("-s  test_abc.py")
    执行结果:
        test_abc.py
        ------->before # 运行第一次
        ------->setup
        ------->test_a
        .------->before # 运行第二次
        ------->setup
        ------->test_b
        .
  • 6.fixture第五个例子(设置作用域为class)
    示例:
        import pytest
        @pytest.fixture(scope='class',autouse=True) # 作用域设置为class,自动运行
        def before():
            print("------->before")
        class Test_ABC:
            def setup(self):
                print("------->setup")
            def test_a(self):
                print("------->test_a")
                assert 1
            def test_b(self):
                print("------->test_b")
                assert 1
        if __name__ == '__main__':
            pytest.main("-s  test_abc.py")
    执行结果:
        test_abc.py
        ------->before # 发现只运行一次
        ------->setup
        ------->test_a
        .
        ------->setup
        ------->test_b
        .
  • 7.fixture第六个例子(返回值)

      示例一:
          import pytest
          @pytest.fixture()
          def need_data():
              return 2 # 返回数字2
    
          class Test_ABC:
    
              def test_a(self,need_data):
                  print("------->test_a")
                  assert need_data != 3 # 拿到返回值做一次断言
    
          if __name__ == '__main__':
              pytest.main("-s  test_abc.py")
      执行结果:
          test_abc.py 
          ------->test_a
          .
    
      示例二:
          import pytest
          @pytest.fixture(params=[1, 2, 3])
          def need_data(request): # 传入参数request 系统封装参数
              return request.param # 取列表中单个值,默认的取值方式
    
          class Test_ABC:
    
              def test_a(self,need_data):
                  print("------->test_a")
                  assert need_data != 3 # 断言need_data不等于3
    
          if __name__ == '__main__':
              pytest.main("-s  test_abc.py")
      执行结果:
          # 可以发现结果运行了三次
          test_abc.py 
          1
          ------->test_a
          .
          2
          ------->test_a
          .
          3
          ------->test_a
          F
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值