Python test framework pytest (16) runs the last failed case, views and clears the cache, and customizes the mark

Keywords: Python unit testing pytest

catalogue

1. Run last failed case

2. View and clear cache  

2.1,--cache-show

2.2,--cache-clear 

3. Custom tag mark  

1. Run last failed case

All use cases are executed, and the first partial use cases fail. At this time, after the tested system is repaired, the last failed use case can be executed.

On the command line, enter pytest -h

You can query two command-line parameters: - lf and -- ff

Parameters:

  • --LF, -- last failed rerun only the use cases that failed last time (or all if there is no failure).

  • --FF, -- failed first run all tests, but first run the tests that failed last time (this may retest, resulting in duplicate fixture setup/teardown).

Create test_lf_ff.py file

Script code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
The official account of WeChat: AllTests software test 
"""

import pytest

@pytest.fixture()
def open():
    name = "AllTests software test "
    return name

def test_case1(open):
    assert open == "AllTests software test "

def test_case2(open):
    assert open == "AllTests"

def test_case3(open2):
    assert open == "AllTests software test "

Open the command line and enter the execute script command:

pytest test_lf_ff.py

Operation results:

Run three test cases for the first time, one passed, one failed and one error.

1. If you only want to run the failed and error cases, use the parameter -- lf

On the command line, enter:

pytest --lf test_lf_ff.py

Operation results:

2. If you want to run the last failed case first and then run other passed cases, use the parameter -- ff

On the command line, enter:

pytest --ff test_lf_ff.py

Operation results:

2. View and clear cache  

After pytest executes the test case, a. Pytest will be generated_ The cache folder of the cache is used to record the last failed use case and ids of the use case.

pytest command line parameters:

  • --Cache show = [cacheshow] displays the contents of the cache and does not execute collection cases or test cases. Optional parameter: glob (default: "*").

  • --Cache clear deletes all cached contents at the beginning of the test run.

Create test_cache.py file

Write 4 test cases

Script code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
The official account of WeChat: AllTests software test 
"""

def test_case1():
    assert 0 == 0

def test_case2():
    assert 0 == 1

def test_case3():
    assert 0 == 0

def test_case4():
    assert 0 == 2

Command line input execution command:

pytest test_cache.py

Operation results:

Two use cases failed and two use cases succeeded.

After running, the project root directory will generate. Pytest_ The cache folder for the cache.

Directory structure:

The lastfailed file records that the previous run case failed

You can see the just executed use cases. Use cases 2 and 4 are failed use cases.

The nodeids file records the nodes of all previous run cases

2.1,--cache-show

Command line input execution command:

pytest --cache-show

Operation results:

Show cached content

2.2,--cache-clear 

Modify test_cache.py file

Script code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
The official account of WeChat: AllTests software test 
"""
def test_case5():
    assert 0 == 0

def test_case6():
    assert 0 == 1

def test_case7():
    assert 0 == 0

def test_case8():
    assert 0 == 2

Command line input execution command:

pytest test_cache.py

Operation results:

Two use cases failed and two use cases succeeded.

View. pytest_cache folder of cache (the file records in it are cumulative)

Lastcompleted file (including the failed case records executed before modifying the test_cache.py file)

nodeids file (including the use case nodes executed before modifying the test_cache.py file)

Use the command line parameter -- cache clear

Command line input execution command:

pytest --cache-clear test_cache.py

Operation results:

Empty all cached contents before executing the use case.

View. Pytest again_ Cache folder for cache

lastfailed file, showing the record of the latest use case failure

nodeids file to display the latest use case nodes

3. Custom tag mark  

pytest can support custom tags, which can divide a project into multiple modules, and then specify the module name for execution.

For example, you can indicate which use cases are executed under Windows and which use cases are executed under Mac. You can specify mark when running code.

Example 1:

1. Create test_mark.py file

Script code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
The official account of WeChat: AllTests software test 
"""

import pytest

@pytest.mark.case1
def test_case1():
    print("====implement test_case1====")

@pytest.mark.case2
def test_case2():
    print("====implement test_case2====")

@pytest.mark.case2
def test_case3():
    print("====implement test_case3====")

@pytest.mark.case3
class TestClass:
    def test_method(self):
        print("====implement test_method====")

def test_noMark():
    print("====No tag test====")

2. Open the command line and enter the execution command:

pytest -s -m case1 test_mark.py

Operation results:

Only the function marked case1 is executed (test_case1).

Example 2:

Or test_mark.py file.

If you don't want to execute, the tag is case1, and everything else is executed, you can directly reverse it.

Open the command line and enter the execution command:

pytest -s -m "not case1" test_mark.py

Operation results:

Except that the tag is case1 (function test_case1) is not executed, all other tags are executed.

Example 3:

Or test_mark.py file.

If you want to execute multiple use cases with custom tags, you can use or

Open the command line and enter the execution command:

pytest -s -m "case2 or case1" test_mark.py

Operation results:

Execute custom tags case1, case2. Note: the order of execution may not be executed before the command.

Example 4:

As in the above examples, how to avoid warnings after execution.

1. Or test_mark.py file. Then create a pytest.ini file (Note: pytest.ini needs to be in the same directory as the running test cases, or it works globally under the root directory).

For example:

Document content:

[pytest]
markers =
    case1: implement case1 Test cases for
    case2: implement case2 Test cases for
    case3: implement case3 Test cases for

2. Open the command line and enter the execution command:

pytest -s -m "case2 or case1" test_mark.py

Operation results:

warnings information is not displayed.

Posted by 87dave87 on Sun, 24 Oct 2021 22:24:26 -0700