Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Statistical code test coverage-Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report