pytest document 37 - Custom use case ordering

Keywords: github Session Python pip

Preface

In the design of test cases, we generally require that there is no order. Use cases can disrupt the execution, so as to achieve the test effect
When some students write use cases, they write the order of use cases. After the order, there will be new problems (for example, the data returned from the previous use case is used as the parameter for the next use case, and so on)
There is a pytest ordering plug-in on github that can control the execution order of use cases. The address of github plug-in is https://github.com/ftobia/pytest-ordering

Environmental preparation

Install dependency package first

pip install pytest-ordering

Use cases

First of all, the default execution order of Py test is in the order of the use cases written in the test_ordering.py file

# test_ording.py
import pytest
# Shanghai - youyou


def test_foo():
    print("Use case 11111111")
    assert True


def test_bar():
    print("Use case 22222222")
    assert True


def test_g():
    print("Use case 333333333333")
    assert True

Operation results

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 Use case 11111111
PASSED
test_ording.py::test_bar Use case 22222222
PASSED
test_ording.py::test_g Use case 333333333333
PASSED

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

Change the order of test cases after using pytest ordering plug-in

# test_ording.py
import pytest
# Shanghai - youyou


@pytest.mark.run(order=2)
def test_foo():
    print("Use case 11111111")
    assert True


@pytest.mark.run(order=1)
def test_bar():
    print("Use case 22222222")
    assert True


@pytest.mark.run(order=3)
def test_g():
    print("Use case 333333333333")
    assert True

Operation results

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 Use case 22222222
PASSED
test_ording.py::test_foo Use case 11111111
PASSED
test_ording.py::test_g Use case 333333333333
PASSED

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

This is the use case executed in the specified order

Posted by Wolverine68 on Wed, 29 Apr 2020 07:55:00 -0700