Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Statistical code test coverage-Python

Shulou Source: shulou.com Published: 2022-06-03 05:08:42 10月04日 Update

To measure the adequacy of Unit Test (unit test), coverage is a necessary index and an important basis for verifying unit test. Here we share the unit test coverage of python unittest coverage.

From the official explanation:

Coverage.py runs on many versions of Python:

CPython 2.6, 2.7 and 3.3 through alpha 3.8.

PyPy2 6.0 and PyPy3 6.0.

Jython 2.7.1, though not for reporting.

IronPython 2.7.7, though not for reporting.

Note: Coverage supports rich versions of python

Install Coverage

Pip install coverage

Whether it is Windows, Linux or Mac os, installation is very convenient, if prompted by pip command not found, please see here or Baidu.

Test the code:

Now that we have installed coverage, we need to use it. Let's create a Module and call mymath.py. The code is as follows:

In this module, we define four methods, namely add, subtract, multiply, divide (+, -, *, /)

Let's test this module and import our module before call. Here is our test code

Use coverage run to run your progame and gather data:

Then we can use Coverage for verification.

Coverage run test_mymath.py

Execute the above command:

If your Module has parameters, you can use the following methods

Coverage run test.py arg1 arg2

Use coverage report to report on the results:

For a nice presentation, use coverage html to get annotated HTML lisitings detailing missed lines:

Coverage html

Using the above command will generate a htmlcov file in the directory containing a detailed test report.

Browser to view report details

It is not difficult to find that in test_mymath.py, we only call the add method of mymath.py, but the other three methods are not called, so the code coverage does not reach 100%. We are prompted with red marks in report, while all the code of test_mymath.py is completely Coverage, with a coverage of 100%.

Such a silly tool uses very easy, through this example of actual combat, let us more intuitively understand the importance of unit test code coverage, peacetime work may be entangled by a variety of business and framework, did not put the center on this, but it is very important, with facts and data words worth thousands of words.

Note: Java also has corresponding Coverage tools such as jacoco, EMMA and so on.

Reference: https://pypi.org/project/coverage/

Today's featured recommendation

JMeter Interface Test-if Controller

JMeter database operation

Jmeter Interface Test-regular expression

JMeter returns garbled code in Chinese

Jmeter interface testing-parameterization

JMeter Interface testing-Basics

Test-impression

Add Wechat to the consulting work

scan a QR code

Welcome self-recommendation and recommendation, need Wechat to push resume!

Please poke the QR code below to learn more.

Tags: Tests code coverage units interfaces methods importance parameters commands tools reports data 2D QR codes work recommendations prompts necessary intuitive thousands of words Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL macOS NVidia Huawei Shulou Technology