zoukankan      html  css  js  c++  java
  • 【pytest学习4】fixture简单使用

    上一篇介绍了如何通过setup和teardown来帮助我们做自动化的前置和后置内容,那么如果有一个场景,有的用例需要用到登录的方法,有的却用不到登录的方法,这时如果用setup和teardown的方法就不能写在一个测试类中,但是pytest中还有更好的前置内容,不需要每个用例都能用到。

    fixture

    fixture属于pytest中的一个方法,fixture是在测试函数运行前后进行执行的,fixture的命名不规范没有强制要求,以及里面的代码内容可以自己进行定制从而满足多种测试需求,配置测试前的数据清理,以及测试完成后的数据管理。fixture中的代码处理测试后的代码为 yield 测试开始前会执行yield前面的代码(setup),测试完成后将执行yield后面的代码(teardown),在fixture中不是强制必须有yield,比如,我只有前置代码,没有后置代码,那就不需要yield内容。或者只有后置代码,没有前置代码。无论测试过程中发生什么错误,yield后续的代码都会被执行

    源码:

    复制代码
    def fixture(  # noqa: F811
        fixture_function: Optional[_FixtureFunction] = None,
        *,
        scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function",
        params: Optional[Iterable[object]] = None,
        autouse: bool = False,
        ids: Optional[
            Union[
                Iterable[Union[None, str, float, int, bool]],
                Callable[[Any], Optional[object]],
            ]
        ] = None,
        name: Optional[str] = None
    ) -> Union[FixtureFunctionMarker, _FixtureFunction]:
    复制代码

    通过上面的源码可以看到fixture一共有5个参数分别是:name,scope,params,autouse,ids。

    栗子:

    有的需要登录,有的必须要登录操作

    复制代码
    import pytest
    
    @pytest.fixture()
    def login():
        print('输入账号,输入密码')
        print('完成登录功能!!!!')
        yield
        print('---退出登录---')
    
    class Test_Login():
    
        def test_01(self,login):
            print('这是用例01')
            print('需要用到登录!')
    
        def test_02(self):
            print('这是用例02')
            print('不需要登录!')
    
        def test_03(self,login):
            print('这是用例03')
            print('这里需要用到登录!')
    
    if __name__ == '__main__':
        pytest.main(['-s','test_03.py'])
    复制代码

     这里会发现,我在用例1和用例3中进行通过fixture进行使用,然后用例1和用例3都执行了配置的登录和退出登录内容。用例2,我没用使用fixture方法,所以没用执行对应代码。

    也可以通过--setup-show的方法来查看详细的fixture信息

    多个fixture使用

    在一个测试用例中可以使用多个fixture的方法,执行顺序根据你传入的顺序进行执行。

    复制代码
    import pytest
    
    @pytest.fixture()
    def login():
        print('输入账号,输入密码')
        print('完成登录功能!!!!')
        yield
        print('---退出登录---')
    
    @pytest.fixture()
    def add():
        print('测试开始执行!')
    
    class Test_Login():
    
        def test_01(self,login,add):
            print('这是用例01')
            print('需要用到登录!')
    
        def test_02(self):
            print('这是用例02')
            print('不需要登录!')
    
        def test_03(self,add,login):
            print('这是用例03')
            print('这里需要用到登录!')
    
    if __name__ == '__main__':
        pytest.main(['-s','test_03.py'])
    复制代码

     

    结果可以看出来,用例1:把login方法的fixture放在了前面,先执行的login方法后执行的add,用例3:add方法在前面,login方法在后面,执行也是add先执行,login后执行。

    fixture相互调用

    其实fixture之间也是可以进行相互调用的。

    复制代码
    import pytest
    
    
    @pytest.fixture()
    def login():
        print('输入账号,输入密码')
        print('完成登录功能!!!!')
        yield
        print('---退出登录---')
    
    
    @pytest.fixture()
    def add(login):
        print('测试开始执行!')
        yield
        print('测试结束!')
    
    class Test_Login:
    
        def test_01(self, add):
            print('------用例01------')
    
        def test_02(self):
            print('------用例02------')
    
        def test_03(self,login):
            print('------用例03------')
    
    
    if __name__ == '__main__':
        pytest.main(['-s', 'test__01.py'])
    复制代码

     通过上面的执行结果可以看到,我们fixture中的login函数被add函数进行调用了,然后在用例中直接执行add,login中的内容也进行了执行

    yield遇到异常后继续执行

    在前面介绍fixture的时候说过,fixture执行过程中,无论遇到什么异常,都会继续执行yeild后面的代码(teardown)

    复制代码
    import pytest
    
    @pytest.fixture()
    def login():
        print('输入账号,输入密码')
        print('完成登录功能!!!!')
        yield
        print('---退出登录---')
    
    class Test_Login():
    
        def test_01(self,login):
            print('这是用例01')
            print('需要用到登录!')
            assert 1==2
    
        def test_02(self):
            print('这是用例02')
            print('不需要登录!')
    
        def test_03(self,login):
            print('这是用例03')
            print('这里需要用到登录!')
    
    if __name__ == '__main__':
        pytest.main(['-s','test_03.py'])
    复制代码

    从例子中很清楚的就发现我们的后置代码在报错的情况下也会执行。在自动化测试过程中,无论测试结果如何,测试数据是肯定能清理的很干净。

    声明 欢迎转载,但请保留文章原始出处:) 博客园:https://www.cnblogs.com/chenxiaomeng/ 如出现转载未声明 将追究法律责任~谢谢合作
  • 相关阅读:
    LintCode "Maximum Gap"
    LintCode "Wood Cut"
    LintCode "Expression Evaluation"
    LintCode "Find Peak Element II"
    LintCode "Remove Node in Binary Search Tree"
    LintCode "Delete Digits"
    LintCode "Binary Representation"
    LeetCode "Game of Life"
    LintCode "Coins in a Line"
    LintCode "Word Break"
  • 原文地址:https://www.cnblogs.com/chenxiaomeng/p/14814840.html
Copyright © 2011-2022 走看看