Bird
Raised Fist0
PyTesttesting~20 mins

Coverage report formats (terminal, HTML, XML) in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Coverage Report Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of pytest coverage terminal report
What is the output of the following pytest coverage command when run in a terminal?

pytest --cov=my_module --cov-report=term
AAn XML file named coverage.xml is created in the current directory.
BAn HTML file named index.html is generated in a directory named htmlcov.
CA summary table printed in the terminal showing coverage percentages per file and total coverage.
DNo output is shown; coverage data is only saved internally.
Attempts:
2 left
💡 Hint
Think about what the 'term' option means for coverage reports.
Predict Output
intermediate
1:30remaining
Result of pytest coverage HTML report generation
After running the command below, what will you find in your project directory?

pytest --cov=my_module --cov-report=html
AA coverage.xml file with coverage data in XML format.
BNo files or directories are created; output is only shown in terminal.
CA coverage.txt file with plain text coverage summary.
DA directory named htmlcov containing an index.html file and other files for viewing coverage in a browser.
Attempts:
2 left
💡 Hint
HTML reports are meant to be viewed in a web browser.
Predict Output
advanced
1:30remaining
Effect of pytest coverage XML report option
What is the effect of running this command?

pytest --cov=my_module --cov-report=xml
ACreates an htmlcov directory with HTML coverage report files.
BGenerates a coverage.xml file in the current directory containing coverage data in XML format.
CDoes not generate any coverage report files.
DPrints coverage summary in the terminal only.
Attempts:
2 left
💡 Hint
XML reports are used for integration with other tools.
🧠 Conceptual
advanced
1:00remaining
Choosing coverage report formats for different needs
Which coverage report format is best suited for viewing detailed coverage information interactively in a web browser?
AHTML report format
BTerminal report format
CXML report format
DNo report format
Attempts:
2 left
💡 Hint
Think about which format uses web pages.
framework
expert
2:00remaining
Combining multiple coverage report formats in pytest
Which command correctly generates both terminal and XML coverage reports in one pytest run?
Apytest --cov=my_module --cov-report=term,xml
Bpytest --cov=my_module --cov-report=term --cov-report=xml
Cpytest --cov=my_module --cov-report=term xml
Dpytest --cov=my_module --cov-report=term+xml
Attempts:
2 left
💡 Hint
Multiple reports are separated by commas without spaces.

Practice

(1/5)
1. What is the main purpose of a coverage report in pytest?
easy
A. To generate test data automatically
B. To run tests faster by skipping some tests
C. To fix bugs in the code automatically
D. To show which parts of the code were tested and which were not

Solution

  1. Step 1: Understand coverage report purpose

    Coverage reports show which lines or parts of code were executed during tests.
  2. 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.
  3. Final Answer:

    To show which parts of the code were tested and which were not -> Option D
  4. Quick 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
A. --cov-report=xml
B. --cov-report=summary
C. --cov-report=html
D. --cov-report=term

Solution

  1. Step 1: Recall pytest coverage report options

    Common options include 'term' for terminal, 'html' for HTML, and 'xml' for XML reports.
  2. Step 2: Match option to HTML report

    Only '--cov-report=html' generates an HTML report.
  3. Final Answer:

    --cov-report=html -> Option C
  4. Quick 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:
pytest --cov=myapp --cov-report=term-missing
What will the terminal output show?
medium
A. An HTML file opened in the browser
B. A summary of coverage with missing lines shown
C. An XML file saved to disk
D. No coverage information displayed

Solution

  1. Step 1: Understand --cov-report=term-missing

    This option shows coverage summary in terminal and highlights missing lines.
  2. 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.
  3. Final Answer:

    A summary of coverage with missing lines shown -> Option B
  4. Quick 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
A. The HTML report is saved in the current directory as 'htmlcov'
B. You forgot to install the pytest-cov plugin
C. You need to add --cov-report=xml to generate HTML
D. HTML reports are not supported by pytest

Solution

  1. Step 1: Check where HTML report is saved

    By default, pytest-cov saves HTML reports in 'htmlcov' folder in current directory.
  2. 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.
  3. Final Answer:

    The HTML report is saved in the current directory as 'htmlcov' -> Option A
  4. Quick 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
A. pytest --cov=myapp --cov-report=xml
B. pytest --cov=myapp --cov-report=html
C. pytest --cov=myapp --cov-report=term
D. pytest --cov=myapp --cov-report=term-missing

Solution

  1. Step 1: Identify XML report option

    The '--cov-report=xml' option generates coverage results in XML format.
  2. Step 2: Match option to CI tool requirement

    CI tools needing XML input require this exact option; others generate HTML or terminal output.
  3. Final Answer:

    pytest --cov=myapp --cov-report=xml -> Option A
  4. Quick 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