Python test framework pytest (21) plug-in - unit test coverage and random execution cases

Keywords: pytest

1. Pytest cov (unit test coverage)

When doing unit testing, we usually refer to code coverage to measure the quality of code.

The pytest cov plug-in can be used to count the unit test coverage.

1.1 installation

Run the following command from the command line to install:

pip install pytest-cov

Or (using the domestic Douban source, the data will be synchronized with the foreign official website regularly, which is fast.)

pip install pytest-cov -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

1.2 example

Create test project, directory structure:

src directory stores the source code of the project

The test directory stores unit test cases

 

1. src directory, create my_status.py file, which is the source code.

The function is implemented to return success or failure information according to different code values.

Script code:

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

def get_status(result):
    if result.get("code") == 000000:
        return "success"
    elif result.get("code") == 100000:
        return "fail: %s" % result.get("msg")
    elif result.get("code") == 100001:
        return "fail: %s" % result.get("msg")
    elif result.get("code") == 100002:
        return "fail: %s" % result.get("msg")
    elif result.get("code") == 200000:
        return "fail: %s" % result.get("msg")
    elif result.get("code") == 200001:
        return "fail: %s" % result.get("msg")
    elif result.get("code") == 200002:
        return "fail: %s" % result.get("msg")
    else:
        return "fail: The system is abnormal. Please try again later"

2. Under the test directory, create test_ my_ The status.py file is a unit test case.

Write a test case to verify that the code value is successful.

Script code:

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

from src.my_status import get_status

def test_get_status_success():
    result = {
        "code": 000000,
        "msg": "success!"
    }
    assert get_status(result) == "success"

3. The command line jumps to the project root directory and enters the execution command (parameter -- cov):

pytest --cov

Operation results:

You can see my in the src directory_ The code coverage of status.py file is 24%, and the coverage of other codes is 100%.

 

4. Generate html coverage report

Enter the execution command (parameter -- cov -- cov report = HTML):

pytest --cov --cov-report=html

Operation results:

 

After execution, the htmlcov report directory will be generated under the root directory of the project.

 

Open the report (the browser opens the index.html file) to view the code coverage.

 

Click the specified file (e.g. my_status.py) to see which codes have not been overwritten (marked in red).

 

5. Specify the running module (package, file)

Enter execution command (parameter -- cov = module):

For example 1:

pytest --cov=src

Operation results:

Specifies to run all modules under the src package

 

Example 2:

pytest --cov=src.my_status

Operation results:

Specify my under the src package to run_ Status.py module

 

 

2. Pytest random order (random execution case)  

By default, pytest runs the use cases in the ASCII coding order named by the module and use case, which means that the order of running the use cases is the same every time.

The pytest random order plug-in can disrupt the execution order of use cases for random execution.

The plug-in allows users to control the level of randomness they want to introduce and disable reordering of test subsets. You can rerun tests in a specific order by passing the seed values reported in the previous test run.

2.1 installation

Run the following command from the command line to install:

pip install pytest-random-order

Or (using the domestic Douban source, the data will be synchronized with the foreign official website regularly, which is fast.)

pip install pytest-random-order -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

2.2 parameters

--Random order uses the default configuration for random testing (it is disabled by default).

 

--random-order-bucket={global,package,module,class,parent,grandparent,none}

Conduct random tests within the specified test range.

Type:

 

 

--random-order-seed=RANDOM_ORDER_SEED uses specific seeds for random testing.

2.3 example  

Create a project with the following directory structure:

 

Create test under module1 package_ Case1.py file, write test cases.

Script code:

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

class TestDemo():
    def test_case1(self):
        print("Execute case 1")

    def test_case2(self):
        print("Execute use case 2")

    def test_case3(self):
        print("Execute use case 3")

Create test under module2 package_ Case2.py file, write test cases.

Script code:

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

class TestClass():
    def test_case4(self):
        print("Execute use case 4")

    def test_case5(self):
        print("Execute use case 5")

    def test_case6(self):
        print("Execute use case 6")

1. Default execution

Open the command line and execute the command under the project root directory

pytest -v

Operation results:

By default, it is executed in sequence.

 

2. Use parameter -- random order

Command line execute command

pytest -v --random-order

Operation results:

Use cases are executed randomly, and -- random order bucket = module is used by default.

--Random order seed = 179023, the seed value will change after each execution.

 

3. Use the parameter -- random order bucket

Command line execute command

pytest -v --random-order-bucket=global

Operation results:

The parameter value is global, which disrupts the execution of all use cases.

 

4. Use parameter -- random order seed

When using random execution, you can use this parameter if you want to follow the order of the last random execution case.

For example, after the last execution, - random order seed = 455773, the seed value 455773 is used.

 

Command line execute command

pytest -v --random-order-seed=455773

Operation results:

According to the random seed, the execution order is the same as the last time.

 

2.4 disable random  

To prohibit random execution of use cases in a module or class, you can use

pytestmark = pytest.mark.random_order(disabled=True)

 

Create test under module1 package_ Case3.py file, write test cases.

Script code:

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

import pytest

pytestmark = pytest.mark.random_order(disabled=True)

def test_case7():
    print("Execute use case 7")

def test_case8():
    print("Execute use case 8")

def test_case9():
    print("Execute use case 9")

Create test under module2 package_ Case4.py file, write test cases.

Script code:

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

import pytest

class TestCase():
    pytestmark = pytest.mark.random_order(disabled=True)

    def test_case10(self):
        print("Execute use case 10")

    def test_case11(self):
        print("Execute use case 11")

    def test_case12(self):
        print("Execute use case 12")

Command line execute command

pytest -v --random-order

Operation results:

Set the use cases that prohibit random execution, which are executed in order.

 

2.5. Disable plug-ins  

If you do not want to use this plug-in, enter the command to disable the plug-in

pytest -p no:random_order

Note: randomization is disabled by default. Pass - p no:random_order you will stop the registration of the plug-in, so its hooks will not be registered, and the command-line options will not appear in -- help.

 


 

Posted by Froy on Fri, 29 Oct 2021 00:51:17 -0700