In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to use Python code coverage tool Coverage, for this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
1. Code coverage
Unit test code coverage, as a measure, can calculate the coverage of the unit test case to the code under test, that is, the ratio of the number of code executed to the total number of code.
Statistics of code coverage are often carried out after unit tests, which can provide a basis for judging the test results.
The most commonly used code coverage statistics tool for Python projects is: Coverage
2. Coverage
Coverage is a tool for counting Python code coverage. It not only supports branch coverage statistics and generates statistical reports in HTML format, but also can be integrated into Jenkins.
Installing Coverage dependencies is also installed using pip
# install Coverage dependency
Pip3 install coverage
Coverage officially provides two ways to count code coverage. They are:
1. Coverage command line
2 、 Coverage API
For a more detailed description, please refer to the official documentation:
Https://coverage.readthedocs.io/en/latest/
3. First of all, write a simple code under test in Python, as follows: # Code under test
# main.py
Def get_level (cource):
"
Custom method
: param cource: grades
: return:
"
If cource > = 90:
Return "excellent"
Elif cource > = 80:
Return "good"
Elif cource > = 60:
Return "qualified"
Elif cource > = 40:
Return "unqualified"
Else:
Return "poor"
Then, write unit test cases
According to the above tested method, here we use the unitte st dependency library that comes with Python to write two simple test cases and specifically cover only two branches of the above method, namely: excellent and good # unit testing
# test_get_level.py
Import unittest
From main import *
Class GetLevel (unittest.TestCase):
Def test_get_level1 (self):
Self.assertEquals (get_level (90), excellent)
Def test_get_level2 (self):
Self.assertEquals (get_level (80), "good")
If _ _ name__ = ='_ _ main__':
Unittest.main (verbosity=2)
Right-click to run the unit test, you will find that the two test cases are passed, then use the Coverage command and API to generate the local code coverage statistics report 1 and the Coverage command
In the root directory of the project, run the coverage run command to generate a .coverage file to collect information on the coverage of the source code being tested.
# 1. Collect the coverage information of the code under test and save it to the .coverage file
Coverage run test_get_level.py
# 2. Generate coverage statistics report
Coverage html-d coverage_result then use the coverage html-d command to generate a code coverage statistics report in the sibling directory
Open the index.html file in the statistical report folder with a browser, where: statements: the total number of lines of code, excluding blank lines and comment lines
Missing: number of lines of code that are not executed
Coverage: code coverage
By clicking on the test_get_level.py file, you can see very intuitively which code is executed and which code is not executed.
2. It is more convenient for Coverage API to use Coverage API to generate code coverage statistics reports. You just need to find the test suite and run it, and then use Coverage API to analyze, save and display.
# generate code coverage statistics reports using API
# exec_api.py
Import coverage
Import unittest
# instantiate an object
Cov = coverage.coverage ()
Cov.start ()
# Test Suite
Suite = unittest.defaultTestLoader.discover (". /", "test_get_level.py")
Unittest.TextTestRunner () run (suite)
# end the analysis
Cov.stop ()
# Save the results
Cov.save ()
# display the result in command line mode
Cov.report ()
# generate HTML coverage report
Cov.html_report (directory='result_html') 4. Last
The above shows how Coverage obtains the code coverage statistical report through a simple Python method combined with the unittest unit test framework
In the actual project, more application scenarios are: Python automation, Django/Flask Web project statistics unit test case code coverage, in order to improve the quality of the product. This is the answer to the question on how to use the Python code coverage tool Coverage. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.