laitimes

pytest测试框架pytest-cov插件生成代码覆盖率

author:Talk to Korey

Pytest provides a variety of plugins to extend its functionality, and this chapter introduces the pytest-cov plugin to generate test coverage reports to help developers understand which parts of the code are covered by tests and which parts need further testing.

pytest测试框架pytest-cov插件生成代码覆盖率

pytest-cov supports a variety of report formats, including plain text, HTML, XML, etc., and developers can choose the appropriate report format according to their needs.

And it can be used with the pytest-xdist plugin introduced in the previous article for distributed testing.

Official Documentation: https://pytest-cov.readthedocs.io/en/latest/index.html

Adaptation Notes:

python >= 3.6

pytest-cov installation

Use the pip command to install: pip install pytest-cov (install the python environment where the pytest project runs, the python installation directory or the virtual environment directory, you can refer to the previous article to view the running environment pycharm and configure the pytest runtime environment)

Install via pycharm: Open the settings and install the plug-in as shown in the figure below (Windows system)

pytest测试框架pytest-cov插件生成代码覆盖率

pytest-cov

You don't need to import it when you use it, you can add parameters directly to the pytest run command. The main parameters are as follows:

--cov=PATH:需要分析的代码路径。

--cov-report=type:要生成的报告类型:html,xml,json,lcov,term,term-missing等。

--cov-config=path覆盖范围的配置文件。 默认.coveragerc。

--no-cov-on-fail: Don't report coverage if the test run fails.

--no-cov:完全禁用覆盖报告。

--cov-reset:重置迄今为止在选项中积累的cov源。

--cov-fail-under=MIN如果总覆盖率小于MIN,则失败。

--cov-append: The result is appended to an existing data file instead of overwriting it.

--non-branch启用分支覆盖。

--cov-context选择设置动态上下文的方法。

Generate a test coverage report

For example, our test project directory is as follows: the src directory has two python program files, and the testcase directory is the test case. Here's what testcode.py and testclass.py in: Simple comparison functions

pytest测试框架pytest-cov插件生成代码覆盖率
pytest测试框架pytest-cov插件生成代码覆盖率

Import two python modules into the test_case2.py file, design two use cases to cover the function function separately, and cover all the code of the function through parameterization. test_case_1 2 sets of parameters are passed, and 1 set of parameters is passed in test_case_2.

pytest测试框架pytest-cov插件生成代码覆盖率
import pytest
import sys
sys.path.append('..')
from src import testcode,testclass

@pytest.mark.parametrize('a,b',((1,1),(2,1)))
def test_case_1(a,b):
     testcode.compare(a,b)

@pytest.mark.parametrize('a,b',((1,1),(3,3)))
def test_case_2(a,b):
      testclass.testclass().compare(a,b)           

Add the parameter -cov=.. to the pytest execution statement. /src --cov-report=html to analyze the code coverage of the src directory and generate an html test report.

After the command is executed, the htmlcov directory is created in the test case directory by default and the html test report is generated.

pytest测试框架pytest-cov插件生成代码覆盖率

Open the index.html report in a browser, and the report will look like this:

Files displays the coverage information of the module.

pytest测试框架pytest-cov插件生成代码覆盖率

Click Functions to view function coverage:

pytest测试框架pytest-cov插件生成代码覆盖率

Click on Classes to see the coverage of the class:

pytest测试框架pytest-cov插件生成代码覆盖率

Click into a file to see the details of the row override in the file: green means overwritten, red is uncovered

testclass file:

pytest测试框架pytest-cov插件生成代码覆盖率

testcode file:

pytest测试框架pytest-cov插件生成代码覆盖率

In some large-scale pytest projects, there are many use cases, and distributed execution is generally used, such as the distributed execution function mentioned at the beginning using the pytest-xdist plug-in, and adding -n 2 to the parameter can be used to execute two processes.

A brief description of the other parameters

--no-cov-on-fail: Don't report coverage if the test run fails.

For example, if you set a use case to fail to execute (for example, add assert False to the above use case), if the execution parameter is marked with --no-cov-on-fail, the coverage report will not be generated if the case execution fails.

pytest测试框架pytest-cov插件生成代码覆盖率

--no-cov:完全禁用覆盖报告。

When this parameter is added to a command, no code coverage report is generated.

--cov-fail-under=MIN如果总覆盖率小于MIN,则认为不达标。

For example, if you add the parameter --cov-fail-under=90 to the command command, the coverage rate must not be less than 90%.

If the command is executed, the following result is displayed: If the coverage rate does not meet the standard, an error message will be reported.

pytest测试框架pytest-cov插件生成代码覆盖率

--cov-context: When we want to accumulate this code coverage in multiple execution cases, we can add the parameter --cov-context=test (the parameter will report an error if other values are set).

Encouragement: Eastern Han Dynasty Bangu "Hanshu Mei Cheng Biography": "The pipe of Mount Tai pierces the stone, and the unipolar is broken and dry." The drill of water is not stone, and the saw of Sophi wood is gradually becoming so. ”

----- refers to the continuous dripping of water droplets, which can drip through stones;

----- metaphor for perseverance, and the collection of small forces can also achieve extraordinary achievements.

---- thank you readers for reading and learning, thank you.

--- wish you all the best in your life!!

Read on