Coverage reports show which parts of your code were tested. They help you find untested code so you can improve your tests.
Coverage report formats (terminal, HTML, XML) in PyTest
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
PyTest
pytest --cov=your_package --cov-report=term pytest --cov=your_package --cov-report=html pytest --cov=your_package --cov-report=xml
term shows coverage summary in the terminal.
html creates a detailed report you open in a browser.
xml creates an XML report for integration with other tools.
Examples
PyTest
pytest --cov=myapp --cov-report=term
htmlcov folder.PyTest
pytest --cov=myapp --cov-report=html
coverage.xml.PyTest
pytest --cov=myapp --cov-report=xml
Sample Program
This simple test checks the add function. Running pytest with coverage and term report shows coverage summary in the terminal.
PyTest
# test_sample.py def add(a, b): return a + b def test_add(): assert add(2, 3) == 5 # Run coverage with terminal report: # pytest --cov=. --cov-report=term
Important Notes
You can combine multiple reports by repeating --cov-report, e.g., --cov-report=term --cov-report=html.
HTML reports are saved in the htmlcov folder by default.
XML reports are useful for CI tools like Jenkins or SonarQube.
Summary
Coverage reports help find untested code.
Terminal reports show quick summaries.
HTML and XML reports provide detailed and shareable formats.
Practice
1. What is the main purpose of a coverage report in pytest?
easy
Solution
Step 1: Understand coverage report purpose
Coverage reports show which lines or parts of code were executed during tests.Step 2: Compare options with coverage purpose
Only To show which parts of the code were tested and which were not correctly describes this purpose; others describe unrelated actions.Final Answer:
To show which parts of the code were tested and which were not -> Option DQuick Check:
Coverage report = tested code visibility [OK]
Hint: Coverage reports show tested vs untested code [OK]
Common Mistakes:
- Confusing coverage with test execution speed
- Thinking coverage generates test data
- Believing coverage fixes bugs automatically
2. Which pytest command option generates an HTML coverage report?
easy
Solution
Step 1: Recall pytest coverage report options
Common options include 'term' for terminal, 'html' for HTML, and 'xml' for XML reports.Step 2: Match option to HTML report
Only '--cov-report=html' generates an HTML report.Final Answer:
--cov-report=html -> Option CQuick Check:
HTML report option = --cov-report=html [OK]
Hint: HTML report uses --cov-report=html option [OK]
Common Mistakes:
- Using --cov-report=xml for HTML report
- Confusing term with html option
- Using non-existent --cov-report=summary
3. Given this pytest command:
What will the terminal output show?
pytest --cov=myapp --cov-report=term-missingWhat will the terminal output show?
medium
Solution
Step 1: Understand --cov-report=term-missing
This option shows coverage summary in terminal and highlights missing lines.Step 2: Match output to options
A summary of coverage with missing lines shown matches terminal summary with missing lines; others describe HTML, XML, or no output.Final Answer:
A summary of coverage with missing lines shown -> Option BQuick Check:
term-missing = terminal summary with missing lines [OK]
Hint: term-missing shows missing lines in terminal [OK]
Common Mistakes:
- Thinking term-missing opens HTML report
- Expecting XML output from term-missing
- Assuming no coverage info is shown
4. You ran
pytest --cov=myapp --cov-report=html but no HTML report was generated. What is the most likely cause?medium
Solution
Step 1: Check where HTML report is saved
By default, pytest-cov saves HTML reports in 'htmlcov' folder in current directory.Step 2: Understand common confusion
Users may think no report generated if they don't check 'htmlcov' folder; plugin installation is needed but question assumes it is installed.Final Answer:
The HTML report is saved in the current directory as 'htmlcov' -> Option AQuick Check:
HTML report folder = htmlcov [OK]
Hint: HTML report saved in 'htmlcov' folder by default [OK]
Common Mistakes:
- Assuming HTML report appears in terminal
- Forgetting to look in 'htmlcov' folder
- Thinking XML option is needed for HTML
5. You want to share coverage results with a CI tool that requires XML input. Which pytest command correctly generates the XML coverage report file?
hard
Solution
Step 1: Identify XML report option
The '--cov-report=xml' option generates coverage results in XML format.Step 2: Match option to CI tool requirement
CI tools needing XML input require this exact option; others generate HTML or terminal output.Final Answer:
pytest --cov=myapp --cov-report=xml -> Option AQuick Check:
XML report option = --cov-report=xml [OK]
Hint: Use --cov-report=xml for CI XML coverage [OK]
Common Mistakes:
- Using HTML or terminal options for XML output
- Not specifying any --cov-report option
- Confusing term-missing with XML
