Pytest-ordering自定义用例执行顺序

我们一般在做自动化测试时,用例设计之间应该是可以相互独立执行的,没有一定的前后依赖关系的,如果我们真的有前后依赖,想指定用例的先后顺序,可以用到pytest-ordering插件解决这个问题

1、安装依赖包
pip install pytest-ordering

2、运用
用例方法上添加装饰器@pytest.mark.run(order=2),用例执行顺序会以order值大小升序去调用执行

3、先按Pytest默认执行顺序(根据用例的先后顺序)先执行了用例1(test_login_01)再执行了用例2(test_login_02)

#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytest


class Test(object):

    def test_login_01(self):
        """用例1"""
        print('执行用例test_login_01断言1')
        pytest.assume(1 == 1)
        print('执行用例test_login_01断言2')
        pytest.assume(2 == 2)

    def test_login_02(self):
        """用例2"""
        print('执行用例test_login_02断言1')
        pytest.assume(3 == 3)
        print('执行用例test_login_02断言2')
        pytest.assume(True)


if __name__ == '__main__':
    pytest.main(['-s', 'test_C_01.py'])



C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/AutoTest/Test/test/test_C_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\admin\Desktop\AutoTest\Test\test
plugins: assume-2.2.1, ordering-0.6
收集的测试用例:[, ]
collected 2 items

test_C_01.py 执行用例test_login_01断言1
执行用例test_login_01断言2
.执行用例test_login_02断言1
执行用例test_login_02断言2
.

============================== 2 passed in 0.04s ==============================

Process finished with exit code 0

4、设置了用例先后顺序为est_login_01(@pytest.mark.run(order=2))、test_login_02(@pytest.mark.run(order=1)),调用后先执行了用例2(test_login_02)再执行了用例1(test_login_01)

#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytest


class Test(object):

    @pytest.mark.run(order=2)
    def test_login_01(self):
        """用例1"""
        print('执行用例test_login_01断言1')
        pytest.assume(1 == 1)
        print('执行用例test_login_01断言2')
        pytest.assume(2 == 2)

    @pytest.mark.run(order=1)
    def test_login_02(self):
        """用例2"""
        print('执行用例test_login_02断言1')
        pytest.assume(3 == 3)
        print('执行用例test_login_02断言2')
        pytest.assume(True)


if __name__ == '__main__':
    pytest.main(['-s', 'test_C_01.py'])


C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/AutoTest/Test/test/test_C_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.4, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\admin\Desktop\AutoTest\Test\test
plugins: assume-2.2.1, ordering-0.6
收集的测试用例:[, ]
collected 2 items

test_C_01.py 执行用例test_login_02断言1
执行用例test_login_02断言2
.执行用例test_login_01断言1
执行用例test_login_01断言2
.

============================== 2 passed in 0.06s ==============================

Process finished with exit code 0

 

你可能感兴趣的:(Pytest-ordering自定义用例执行顺序)