pytest的fixture的使用(四)

pytest的fixture的使用

1、@fixture(scope=‘function’, params=None, autouse=False, ids=None, name=None)

  • scope:
    • function(默认函数级别,重要)
    • class(重要)
    • module
    • package
    • session
  • params:一个可选的参数列表,它将导致对fixture函数和使用它的所有测试的多次调用。当前参数在request.param中可用。
  • autouse:如果为真,则对所有可以看到它的测试激活fixture func。如果为False(默认值),则需要显式引用来激活装置。

2、使用fixture方法:通过参数调用。

  • 优点:当fixture有返回值,或者参数时,只能使用这种方式

  • fixture在类内和类外,调用方式不变(默认函数级别)

    import pytest
    
    @pytest.fixture()
    def fixture1():
        print("---函数级别的fixture---")
    
    class Test_01(object):
        # @pytest.fixture()
        # def fixture1(self):
        #     print("---函数级别的fixture---")
      
        def test_a(self, fixture1):
            print("------测试A函数---------")
      
        def test_b(self):
            print("------测试B函数---------")
        if name == 'main':
            pytest.main(["-sv", "test_01.py"])
    
  • 类级别(多个test函数使用时,只会在第一个调用的时候,出现一次。一个test使用时,跟函数级别差别不大)

    import pytest
    
    
    @pytest.fixture(scope="class")
    def fixture1():
        print("---类级别的fixture---")
    
    
    class Test_01(object):
        def setup(self):
            print("------setup-----")
    
        def test_a(self):
            print("------测试A函数---------")
    
        def test_b(self,fixture1):
            print("------测试B函数---------")
    
        def test_c(self,fixture1):
            print("------测试C函数---------")
    if __name__ == '__main__':
        pytest.main(["-sv", "test_01.py"])
    
    
    test_01.py::Test_01::test_a ------setup-----
    ------测试A函数---------
    PASSED
    test_01.py::Test_01::test_b ---函数级别的fixture---
    ------setup-----
    ------测试B函数---------
    PASSED
    test_01.py::Test_01::test_c ------setup-----
    ------测试C函数---------
    PASSED
    
  • 有返回值的fixture

    import pytest
    
    
    @pytest.fixture()
    def fixture1():
        return 2,3
    
    
    class Test_01(object):
        def setup(self):
            print("------setup-----")
    
        def test_a(self,fixture1):
            print("------测试A函数---------")
            print(fixture1)
    if __name__ == '__main__':
        pytest.main(["-sv", "test_01.py"])
    
    test_01.py::Test_01::test_a ------setup-----
    ------测试A函数---------
    (2, 3)
    PASSED
    
  • 有参数的fixture

    import pytest
    
    
    @pytest.fixture(params=(2, 3))
    def fixture1(request):
        return request.param
    
    
    class Test_01(object):
        def setup(self):
            print("------setup-----")
    
        def test_a(self, fixture1):
            print("------测试A函数---------")
            print(fixture1)
    
    
    if __name__ == '__main__':
        pytest.main(["-sv", "test_01.py"])
    
    test_01.py::Test_01::test_a[2] ------setup-----
    ------测试A函数---------
    2
    PASSED
    test_01.py::Test_01::test_a[3] ------setup-----
    ------测试A函数---------
    3
    PASSED
    

  • autouse-自动化运行fixture

    import pytest
    
    
    @pytest.fixture(autouse=True,scope="class")
    def fixture1():
        print("------类级别的fixture-------")
    
    
    class Test_01(object):
        def setup(self):
            print("------setup-----")
    
        def test_a(self):
            print("------测试A函数---------")
    
        def test_b(self):
            print("------测试B函数---------")
    
    if __name__ == '__main__':
        pytest.main(["-sv", "test_01.py"])
    
    test_01.py::Test_01::test_a ------类级别的fixture-------
    ------setup-----
    ------测试A函数---------
    PASSED
    test_01.py::Test_01::test_b ------setup-----
    ------测试B函数---------
    PASSED
    

3、通过@pytest.mark.usefixtures调用

  • 如果有参数传递,或者返回值时无法实现

  • 函数级别

    import pytest
    
    
    @pytest.fixture()
    def fixture1():
        print("------函数级别的fixture-------")
    
    
    @pytest.mark.usefixtures("fixture1")
    class Test_01(object):
        def setup(self):
            print("------setup-----")
    
        # @pytest.mark.usefixtures("fixture1")
        def test_a(self):
            print("------测试A函数---------")
    
        # @pytest.mark.usefixtures("fixture1")
        def test_b(self):
            print("------测试B函数---------")
    
    
    if __name__ == '__main__':
        pytest.main(["-sv", "test_01.py"])
    
    
    test_01.py::Test_01::test_a ------函数级别的fixture-------
    ------setup-----
    ------测试A函数---------
    PASSED
    test_01.py::Test_01::test_b ------函数级别的fixture-------
    ------setup-----
    ------测试B函数---------
    PASSED
    
  • 类级别

    import pytest
    
    
    @pytest.fixture(scope="class")
    def fixture1():
        print("------类级别的fixture-------")
    
    
    @pytest.mark.usefixtures("fixture1")
    class Test_01(object):
        def setup(self):
            print("------setup-----")
    
        def test_a(self):
            print("------测试A函数---------")
    
    
        def test_b(self):
            print("------测试B函数---------")
    
    
    if __name__ == '__main__':
        pytest.main(["-sv", "test_01.py"])
    
    test_01.py::Test_01::test_a ------类级别的fixture-------
    ------setup-----
    ------测试A函数---------
    PASSED
    test_01.py::Test_01::test_b ------setup-----
    ------测试B函数---------
    PASSED
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值