Python Statistics Unit test code coverage

Source: Internet
Author: User

1.1.1 Implementation Process

    1. Preparatory work
      First, before running the command, we need to install a package: coverage
      The command is as follows:
      PIP Install coverage # (PY-3-m pip install coverage)
      After successful installation, successfully and other words will appear.

    2. Implementing code Coverage commands
      There are 3 of the commands involved, which are given here first, followed by the use of examples to do a detailed explanation:
      The first command is to run your test script file, the second command prints the coverage report information in the console, the third command generates a Htmlcov folder in the sibling directory, and opens the index.html in the folder to view code coverage in a graphical manner.
      coverage?run?xxx.py
      Coverage report-m
      Coverage HTML

    3. Code Coverage-Practical examples
      For example: I want to test the code coverage of my local operate_calculate.py code, which can be done as follows:
      Command one: Coverage run operate_calculate.py

Command two: Coverage report-m

Command three: Coverage html

As a result, a Htmlcov folder is generated, and the contents of the folder are as follows:

Open the index.html and you'll see beautiful test reports:

Click on the specific module to view specific information, such as Click calc.py:

The red part is the line of code that is not executed.

This is what Python uses coverage packages to achieve code coverage.

Python Statistics Unit test code coverage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.