Pytest’s use case tagging and test execution

Pytest can pass data into the test function through tags, and can also filter the executed use cases through tags.

1. Built-in tags in pytest

To use pytest marks, you need to use pytest.mark.marks. There are many built-in marks in pytest to cope with various test scenarios.

1.1, pytest.mark.parametrize: mark for use case parameterization

Parametrize can separate the use case data and the logic code of use case execution, and automatically generate test cases based on the use cases.

Demo:

@pytest.mark.parametrize('item',[11,22,33,44,55,66])
def test_demo(item)
assert item > 50

1.2, pytest.mark.skip: skip use case execution

Use cases decorated with skip will be unconditionally skipped during execution.

Parameter reason: the reason for skipping the test function.

Demo

# Do not write skip reason
@pytest.mark.skip
def test_demo()
assert item > 50

#Write skip reason
@pytest.mark.skip(reason='No need to execute')
def test_demo()
assert item > 50

1.3, pytest.mark.skipif: skip use cases based on conditions

skipif can decide whether to skip the execution of the test case based on the condition. If the condition is True, the test function execution will be skipped.

Parameters: condition -condition to skip

Parameters: reason – Reason for skipping

Demo

a = 10
@pytest.mark.skipif(a > 20,reason='The condition is not established, do not execute')
def test_demo()
assert item > 50

1.4, pytest.mark.xfail: mark use cases that are expected to fail

xfail can mark a test function as a test case that is expected to fail.

Parameters: condition – the condition (True/False) that marks the test function as xfail

Parameters: reason – the reason why the test function was marked as xfail

Parameters: raises -Exception types for expected failures

Parameters: run – whether the test function should actually be executed. If False, the function will always xfail and will not be executed.

Parameters: strict – strict mode (True/False)

Demo

a = 10
@pytest.mark.xfail(a > 20, reason='The condition is not established, no execution' raises=AssertionError)
def test_demo()
assert item > 50

1.5, pytest.mark.usefixtures: Set test fixtures for test classes or modules

The usefixtures tag is generally used to uniformly set test fixtures for the test methods under the test class.

Demo

# All test cases of the test class TestDome execute the fixture my_fixture
@pytest.mark.usefixtures('my_fixture this fixture')
class TestDome:
    # Function case specifies test fixture
    def test_02(self):
        print('----Test case: test_01------')

    # Function case specifies test fixture
    def test_03(self):
        print('----Test case: test_02------')

2. Custom tags

pytest supports registering custom tags through the pytest.ini file. Use tags are used to filter use cases when executing them. ,

2.1. Registration mark

The syntax of the pytest.ini file registration tag is as follows:

[pytest]

markers =
    Mark 1
    Mark 2

2.2. Mark function

Demo:

# Load the label in front of the use case: @pytest.mark.label name
@pytest.mark.main
 def test_demo():
    pass

2.3. Tag class

Demo:

# Method 1: Mark directly on the class
@pytest.mark.main
class TestClass(object):
    def test_demo1(self):
        assert 10 > 20
  
# Method 2: Through the class attribute pytestmark, you can add multiple markers at the same time
class TestClass(object):
    pytestmark = [pytest.mark.main, pytest.mark.main]
  
    def test_demo1(self):
        assert 10 > 20

3. Filter use case execution through tags

Demo: The existing use cases are as follows:

import pytest

@pytest.mark.yuze
@pytest.mark.musen
def test_01():
    print("Use Case 1")

def test_02():
    print("Use Case 2")

@pytest.mark.musen
def test_03():
    print("Use case three")

@pytest.mark.musen
def test_04():
    print("Use Case 4")

@pytest.mark.yuze
def test_05():
    print("Use case five")

@pytest.mark.yuze
def test_06():
    print("Use Case 6")

There are 6 test cases in the demo above, which are marked by pytest.mark.yuze and pytest.mark.musen respectively. Next, let’s take a look at how to select test cases for execution through marking.

3.1. Filter by single tag

Syntax: pytest -m ‘tag name’

Demo: pytest -m musen

The execution results are as follows:

========================== test session starts ================== ========
platform win32 -- Python 3.7.3, pytest-5.4.2, py-1.8.0, pluggy-0.13.0
rootdir: C:\project\, inifile: pytest.ini
plugins: allure-pytest-2.8.15, Faker-8.11.0, metadata-1.9.0, parallel-0.0.8, repeat-0.8.0, rerunfailures-9.0, testreport-1.1.2
collected 6 items / 3 deselected / 3 selected
test_mode.py ... [100%]
========================== 3 passed, 3 deselected in 0.29s ================ ==========

It can be seen that the execution result shows that 3 use cases were executed and 3 were not selected.

3.2. Select multiple markers at the same time

Syntax: pytest -m “mark 1 or mark 2”

Command: pytest -m “musen ro yuze”

Execute use cases tagged with musen or yuze. The execution results are as follows:

========================== test session starts ================== ========
platform win32 -- Python 3.7.3, pytest-5.4.2, py-1.8.0, pluggy-0.13.0
rootdir: C:\project\, inifile: pytest.ini
plugins: allure-pytest-2.8.15, Faker-8.11.0, metadata-1.9.0, parallel-0.0.8, repeat-0.8.0, rerunfailures-9.0, testreport-1.1.2
collected 6 items / 1 deselected / 5 selected
test_mode.py ..... [100%]
========================== 5 passed, 1 deselected in 0.29s ================ ==========

As can be seen from the above results, as long as either musen or yuze is added,

Syntax: pytest -m “mark 1 and mark 2”

Command: pytest -m “musen and yuze”

Execute use cases tagged with both musen and yuze tags. The execution results are as follows

========================== test session starts ================== ========
platform win32 -- Python 3.7.3, pytest-5.4.2, py-1.8.0, pluggy-0.13.0
rootdir: C:\project\, inifile: pytest.ini
plugins: allure-pytest-2.8.15, Faker-8.11.0, metadata-1.9.0, parallel-0.0.8, repeat-0.8.0, rerunfailures-9.0, testreport-1.1.2
collected 6 items / 5 deselected / 1 selected
test_mode.py .[100%]
========================== 1 passed, 5 deselected in 0.29s ================ ==========

Finally: The following is the supporting learning materials. For friends who do [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you. ! [100% free without any tricks]

Software testing interview applet

A software test question bank that has been used by millions of people! ! ! Who is who knows! ! ! The most comprehensive interview test mini program on the Internet, you can use your mobile phone to answer questions, take the subway, bus, and roll it up!

Covering these interview question sections:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. Web, app, interface automation, 7. Performance testing, 8. Programming basics, 9. HR interview questions, 10. Open test questions, 11. Security testing, 12. Computer basics

How to obtain the full set of information: Click on the small card below to get it yourself