♥ preface
In the introduction of the last article, we introduced the pre and post methods and fixture mechanism of pytest. This chapter mainly introduces the marking mechanism and use case execution method in pytest. Pytest can pass the data into the test function through the tag, or filter the executed use cases through the tag, and then go directly to the topic.
1, Built in tags in pytest
The use of pytest tag needs to pass pytest mark. In order to deal with various test scenarios, pytest also has many built-in tags.
1.1,pytest. mark. Parameterize: mark of use case parameterization
By parameterizing, you can separate the use case data from the logic code executed by the use case, and automatically generate test cases according to 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 case execution
Use cases decorated with skip will be skipped unconditionally during execution,
Parameter reason: 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 execution required') 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 use case according to the condition. If the condition is True, the execution of the test function will be skipped.
Parameter: condition - condition to skip
Parameter: reason - reason for skipping
Demo
a = 10 @pytest.mark.skipif(a > 20,reason='If the condition is not established, it will not be implemented') def test_demo() assert item > 50
1.4,pytest.mark.xfail: marks the use case of the expected failure
xfail can mark a test function as a use case for an expected execution failure.
Parameter: condition - condition to mark the test function as xfail (True/False)
Parameter: reason - the reason why the test function is marked as xfail
Parameter: raises - exception type of expected failure
Parameter: run - whether the test function should actually be executed. If False, the function will always xfail and will not be executed.
Parameter: strict -- strict mode (True/False)
Demo
a = 10 @pytest.mark.xfail(a > 20,reason='If the condition is not established, it will not be implemented' raises=AssertionError ) def test_demo() assert item > 50
1.5,pytest. mark. Use fixtures: 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 TestDome test class execute my_fixture this fixture @pytest.mark.usefixtures('my_fixture This fixture') class TestDome: # Function case specifies the test fixture def test_02(self): print('----Test case: test_01------') # Function case specifies the test fixture def test_03(self): print('----Test case: test_02------')
2, Custom tag
Pytest supports passing pytest INI file to register custom tags. To meet the requirements of filtering use cases through tags when executing use cases.,
2.1 registration mark
pytest. The syntax of the INI file registration tag is as follows:
[pytest] markers = Mark 1 Mark 2
2.2 marking function
Demo:
# Load label before use case: @ pytest mark. Tag name @pytest.mark.main def test_demo(): pass
2.3 marking
Demo:
# Method 1: mark the class directly @pytest.mark.main class TestClass(object): def test_demo1(self): assert 10 > 20 # Method 2: multiple tags can be added at the same time through the class attribute pytestmark class TestClass(object): pytestmark = [pytest.mark.main, pytest.mark.main] def test_demo1(self): assert 10 > 20
3, Filter case execution by 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 3") @pytest.mark.musen def test_04(): print("Use case 4") @pytest.mark.yuze def test_05(): print("Use case 5") @pytest.mark.yuze def test_06(): print("Use case 6")
There are 6 test cases in the above Demo, which pass pytest mark. Yuze and pytest mark. Musen has been tagged. Next, let's take a look at how to select use case execution through tags.
3.1. Screening by single marker
Syntax: pytest -m 'tag name'
Demo: pytest -m musen
The 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 ==========================
You can see that three use cases are executed and three are not selected.
3.2. Select multiple tags at the same time
Syntax: pytest -m "tag 1 or tag 2"
Command: pytest -m "musen ro yuze"
Execute use cases marked by musen or yuze. The 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 "tag 1 and tag 2"
Command: pytest -m "musen and yuze"
Execute the use cases marked simultaneously by musen and yuze. The 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 ==========================
Now there is such an opportunity. I invite you to join our software testing learning exchange group: 914172719. Note "csdn". You can discuss and exchange software testing together, learn software testing technology, interview and other aspects of software testing together, and have free live classes to gain more testing skills. Let's advance Python automated testing / test development together, The road to high pay.
Let's give you a word of encouragement: when our ability is insufficient, the first thing to do is internal practice! When our ability is strong enough, we can look outside!
Finally, we have prepared a matching learning resource for you. You can get a 216 page Software Test Engineer Interview document information without official account: heartbroken spicy bar. And the corresponding video learning tutorials for free!, The materials include basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, special topics of packet capture tools, interface test tools, test advanced Python programming, Web automation test, APP automation test, interface automation test, advanced test continuation, test architecture, development test framework, performance test, security test, etc.
Haowen recommendation
What kind of person is suitable for software testing?
The man who left work on time was promoted before me
The test post changed jobs repeatedly and disappeared
"After a year in office, the automation software test hired by high salary was persuaded to quit."