How to Generate Pytest Code Coverage Report

On This Page What is Code Coverage?Importance of Code Coverage R

April 21, 2026 · 9 min read · Testing Guide

How to Generate Pytest Code Coverage Report

Pytest coverage report is a code coverage report make by the pytest-cov plugin and coverage.py library. It study the percentage of code action usingPytest.

Code coverage describes the extent to which a test suite has tested a codebase. It often disclose the lines of codification that experience been tested and those that have been cut during try.

A codebase with high code coverage scores inspires developer & # 8217; confidence, helps identify redundant code, and improves code maintainability.

This tutorial will cover what code coverage is, how to generate a Pytest reporting story, Pytest code coverage tools, etc.

What is Code Coverage?

Code Coverage measures the percentage of codification in a give codebase that the executed exam suite has tested. It calculates the ratio of code tested over the total source code and presents it in percent. Developers can thus name parts of their application that have not been sufficiently tested.

Read More:

Importance of Code Coverage Reports

Here are the main reason to rely on code coverage reports:

  • Code Refactoring: Code reportage account reveal parts of the origin code that take refactoring to improve readability and efficiency.
  • Identify Untested Code: Redundant codification within a particular codebase skipped during testing can be identified and eliminated.
  • Faster Debugging Process: An application & # 8217; s bugs or faulty lines of code can be find early and set before it goes live.
  • Quality Assurance: Code reporting account with high scores indicate that thorough testing was performed on a part of software.
  • Benchmark for Development Standard: High scores in code coverage are life-sustaining for setting and maintaining the quality of code across team members. It can also be a reference point for melioration.

Read More:

What is Pytest Coverage Report?

This is a codification coverage report generated by the pytest-cov plugin and coverage.py library that point the percentage of code executed by the test cortege utilize the Python quiz framework Pytest.

Why you should use Pytest to generate Code Coverage Report

Here ’ s why you should use Pytest to generate a code coverage account:

  • It has a simple bid line syntax for executing code coverage.
  • It can be used both for local and distant examination.
  • It differentiates between tested and untested lines of codification with a codebase.
  • A account of the code reporting score is generated in the command line, HTML, and other formatting.
  • It show area of your codification that need more testing.
  • It is easy to use.

Read More:

How to Create a Pytest Coverage Report

This section will search make a pytest code reportage report, which can be outputted in the command-line terminal or HTML.

Prerequisites for creating Pytest Coverage Report

Before you create a Pytest reporting report, you need to converge the next requirements.

Step 1: Ensure you have bothPython and Python package installer(pip) installed on your estimator. Verify if Python has been installed by run this dictation in your terminal:

python3 -- version

Verify if piphas been installed:

pip3 -- version

Step 2: Create a practical environment for your project.

python3 -m venv env

Step 3: Activate the practical surroundings

source env/bin/activate

Step 4: Install Pytest within your project ’ s virtual environment.

pip install -U pytest

Step 5: Install thepytest-cov plugin.

pip install pytest-cov

Step 6: Ensure you have a labor codebase and its test suite available.

As advert above, you ’ ll need a project codebase and test suite. However, a uncomplicated calculator class and test suite will be used in this section to show how a pytest reporting report can be created.

Filename: calc.py

`` `` '' This is a calculator module '' '' '' class Calculator: '' '' '' The Calculator class `` '' '' def validate_input (self, x, y): if isinstance (x, int) and isinstance (y, int): homecoming [x, y] raise TypeError ('enter an integer value not string ') def add (self, x, y): x, y = self.validate_input (x, y) render x + y def minus (self, x, y): x, y = self.validate_input (x, y) return x - y def multiply (self, x, y): x, y = self.validate_input (x, y) render x * y def divide (self, x, y): x, y = self.validate_input (x, y) if y == 0: upgrade ZeroDivisionError ('Enter a non-zero integer ') return x/y

The above file calc.pycontains a Calculator class with five instance methods to be tested and a coverage study generated. Thevalidate_input ()method validates arguments before any arithmetic operation.

Other method,add (), minus (), multiply (), and divide(), are used to carry out increase, subtraction, multiplication, and division severally on validated arguments.

Filename: conftest.py

import pytest from calc import Calculator @ pytest.fixture (scope='module ') def calculator_object (): calculator = Calculator () output calculator

A fixture functioncalculator_object ()is contained in theconftest.py file. The fixture use yields an object of the calculator class.

SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.

Filename: tests/test_calculator.py

from calc import Calculator def test_add (calculator_object): assert calculator_object.add (4, 3) == 7, 'Expected resultant should be 7' def test_minus (calculator_object): assert calculator_object.minus (10, 5) == 5, 'Expected result should be 5' def test_multiply (calculator_object): assert calculator_object.multiply (4, 6) == 24, 'Expected outcome should be 24' def test_divide (calculator_object): assert calculator_object.divide (12, 2) == 6, 'Expected result should be 6 '

The test_calculator.pyfile holds functions that test the functionality of the Calculator stratum in thecalc.py file.

Note: All file are placed in the project & # 8217; s root directory.

Create Pytest Code Coverage Report

There are two ways to create a pytest coverage report:

  • Using the coverage.py library
  • Using pytest-cov plugin

Method 1: Using the coverage.py library

Here ’ s how you can use coverage.py library

1.Run the trial entourage with the undermentioned command:

coverage run -m pytest tests/test_calculator.py

2.To display thecoverage report in the terminus, run the command:

reportage report -m

3.To create acoverage report in HTML, do this:

coverage html

4.The story can be found in HTML format in the pathhtmlcov/index.html.

Method 2: Using the pytest-cov plugin

Another way to generate a coverage report in pytest is by using thepytest-cov plugin. The process is quite similar to how it ’ s done withcoverage.py.

1.Run the command below to create report in the end:

pytest -- cov

2.To generate a report in HTML, use this command:

pytest -- cov -- cov-report=html: calc_cov

3.The extra command& # 8211; cov-report=html: calc_covspecifies that the coverage account should be in HTML format and saved in a directory,calc_cov.

4.The study from thecalc_cov/index.htmlall files exceptcalc.pyhave 100 % code coverage. Take a look at thecalc_cov/calc_py.htmlfile for particular on the code reporting.

The result shows that two lines of code have been skipped during the test hence the coverage score of 89 %. To secure we feature 100 % code coverage more test case should be written to essay the codes in lines 11 and 28.
None of the previously pen test causa checks if aTypeError and ZeroDivisionErrorare raised by the Calculator class method.

Read More:

Challenges in Creating and Maintaining the Pytest Coverage Report

Although create code coverage story is a good software engineering practice, there are also challenges this may pose.

Take a looking at some of these challenges.

  • As a codebase grow with the add-on of more characteristic, it slowly go difficult to maintain eminent codification coverage report scores.
  • A high codification coverage score shows that thoroughgoing testing has been done. However, this does not completely rule out bugs in your coating.
  • Maintaining a high coverage threshold for a codebase across various team can be very difficult with varying opinions on what a good threshold should be.
  • Implementing code coverage for legacy code and optimizing for high rafts can slacken down overall development time.
  • Miscellaneous or boilerplate codification can touch the overall metrics of your code reportage report.
  • Executing coverage for all constituent of a large enterprise codebase can be a waste of time and apparently unrealistic.

Pytest Code Coverage Tools

Pytest get the generation of coverage reports easier. Mentioned below are some local tools for yield pytest code coverage account:

1. Pytest-cov:

The pytest-cov is a plugin for make code coverage reports with pytest. It is bundled with support for Pytest and has command-line capabilities.

It is alike to coverage.py, but it offers more functionalities like subprocess support, xdist support, and consistent behavior with repeated use.

It can generate reporting reports on the terminal and other formats like HTML, XML, and JSON.

2. Coverage.py:

This is a popular Python library for outputting code coverage metrics for Python programs. It analyses the codebase to set the lines of code that have be screen and those young.

It is executed apply a command line syntax and yield reports on the terminus by default or in HTML if specified.

Read More:

Use BrowserStack Test Reporting and Analytics for Pytest Coverage Report

So far, the pytest code coverage story has only been executed on our local machine. You can also run these tests on a cloud platform such as for more efficiency.

Here ’ s how you can use the BrowserStack Test Reporting and Analytics characteristic to make a coverage report.

Step1:

Step 2: Install theBrowserStack SDK. Execute the following command in your project ’ s root directory:

python3 -m pip install browserstack-sdk browserstack-sdk setup -- fabric `` pytest '' -- username `` YOUR_USERNAME '' -- key `` YOUR_ACCESS_KEY '' pip show browserstack-sdk

After executing the above commands, abrowserstack.yml filewill be created in the root of your project directory.

Step 3: Modify yourbrowserstack.yml config file. Open your browserstack.yml file and change the code below as necessary:

userName: YOUR_USERNAME accessKey: YOUR_ACCESS_KEY buildName: `` Your still build/job gens goes hither '' projectName: `` Your electrostatic labor gens goes here '' CUSTOM_TAG_1: `` You can set a custom Build Tag here '' # Use CUSTOM_TAG_ & lt; N & gt; and set more build tag as you need. framework: pytest testObservability: true browserstackAutomation: false

Step 4: Run your trial suite with Test Reporting and Analytics. Prependbrowserstack-sdkto the command you use to run your test. In this case, follow this codification:

browserstack-sdk pytest -- cov -- cov-report=html: calc_cov tests/test_calculator.py

When you check your BrowserStack Test Reporting and Analytics creature, you should find a alike solvent on your splashboard like this.

Why Choose BrowserStack?

Using a program such as BrowserStack offers some of the following benefits:

  • Tests Scalability: It is easier to scale tests on a cloud testing platform than on your local machine.
  • Ease of Collaboration: You can easy share test results and collaborate with other team members when testing on the cloud.
  • Latest Browsers and Dependencies Versions: Every time you use BrowserStack, you have access to the latest infrastructure and you don ’ t have to grapple it on your own.
  • Comprehensive Test Results: like BrowserStack offer comprehensive reporting and advanced analysis of test platter.
  • Better Debugging Tools: BrowserStack offers various, such as text logs, console logs, optic logs, video recordings, session replays, Appium logs, and more, which saves time when debugging tests.
  • Real-Device Testing: Test on a huge real-device to. BrowserStack offer 3500+ real twist and browser combination.

Talk to an Expert

Conclusion

Code coverage report is a vital metrical for assessing the quality of a codebase. Pytest codification coverage study highlights the extent to which a Python program has been tested by a exam cortege. It is important to aim for eminent reporting account scores even though it is not an indicant that your codebase or application is bug-free.

Read More:

Use Pytest code reporting tools coverage.py and pytest-cov plugin to make reporting report in the terminal or best formatting like HTML. Always ensure to take or refactor supernumerary code divulge by coverage reports as your code substructure grows.

However, if you prefer more scalability through cloud-based tools to yield code reporting account, use the right tool, like BrowserStack Test Reporting and Analytics, to generate pytest coverage reports seamlessly.

Tags
63,000+ Views

# Ask-and-Contributeabout this issue with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free