pytest文档37-自定义用例顺序(pytest-ordering)

前言

测试用例在设计的时候,我们一般要求不要有先后顺序,用例是可以打乱了执行的,这样才能达到测试的效果.
有些同学在写用例的时候,用例写了先后顺序, 有先后顺序后,后面还会有新的问题(如:上个用例返回数据作为下个用例传参,等等一系列的问题。。。)
github 上有个 pytest-ordering 插件可以控制用例的执行顺序,github插件地址https://github.com/ftobia/pytest-ordering

环境准备

先安装依赖包

pip install pytest-ordering

使用案例

先看pytest默认的执行顺序,是按 test_ording.py 文件写的用例先后顺序执行的

# test_ording.py
import pytest
# 上海-悠悠


def test_foo():
    print("用例11111111111")
    assert True


def test_bar():
    print("用例22222222222")
    assert True


def test_g():
    print("用例333333333333333")
    assert True

运行结果

D:\demo>pytest test_ording.py -vs
============================= test session starts =============================
platform win32 -- Python 3.6.0
cachedir: .pytest_cache
metadata: 
plugins: ordering-0.6,
collected 3 items

test_ording.py::test_foo 用例11111111111
PASSED
test_ording.py::test_bar 用例22222222222
PASSED
test_ording.py::test_g 用例333333333333333
PASSED

========================== 3 passed in 0.07 seconds ===========================

使用 pytest-ordering 插件后改变测试用例顺序

# test_ording.py
import pytest
# 上海-悠悠


@pytest.mark.run(order=2)
def test_foo():
    print("用例11111111111")
    assert True


@pytest.mark.run(order=1)
def test_bar():
    print("用例22222222222")
    assert True


@pytest.mark.run(order=3)
def test_g():
    print("用例333333333333333")
    assert True

运行结果

D:\demo>pytest test_ording.py -vs
============================= test session starts =============================
platform win32 -- Python 3.6.0
cachedir: .pytest_cache
metadata: 
plugins: ordering-0.6,
collected 3 items

test_ording.py::test_bar 用例22222222222
PASSED
test_ording.py::test_foo 用例11111111111
PASSED
test_ording.py::test_g 用例333333333333333
PASSED

========================== 3 passed in 0.04 seconds ===========================

这样就是按指定的顺序执行的用例

猜你喜欢

转载自www.cnblogs.com/yoyoketang/p/12798200.html