pytest-html helps you create easy-to-read HTML reports after running tests. This makes it simple to see which tests passed or failed in a clear webpage format.
pytest-html for HTML reports
pytest --html=report.html
This command runs your tests and creates an HTML report named report.html.
You can open the report in any web browser to see the test results.
report.html in the current folder.pytest --html=report.html
tests/ folder and save the report in the reports/ folder.pytest tests/ --html=reports/my_report.html
pytest --html=report.html --self-contained-html
This simple test file has two tests. Running pytest with --html=report.html creates an HTML report showing both tests passed.
def test_addition(): assert 2 + 3 == 5 def test_subtraction(): assert 5 - 2 == 3 # Run this in terminal: # pytest --html=report.html
Make sure to install pytest-html first using pip install pytest-html.
You can customize the report with extra options like adding environment info or screenshots.
Open the generated HTML file in a browser to view the report visually.
pytest-html creates easy-to-read HTML reports for your test runs.
Use pytest --html=filename.html to generate the report.
Reports help share and understand test results quickly.
Practice
pytest-html in testing?Solution
Step 1: Understand pytest-html functionality
pytest-html is a plugin that creates HTML reports summarizing test results.Step 2: Identify the main use
The main use is to generate readable reports, not to speed up tests or convert code.Final Answer:
To generate easy-to-read HTML reports of test results -> Option DQuick Check:
pytest-html = HTML reports [OK]
- Thinking pytest-html speeds up tests
- Confusing report generation with test execution
- Assuming tests are written in HTML
report.html using pytest?Solution
Step 1: Recall pytest-html command syntax
The correct syntax to generate an HTML report ispytest --html=filename.html.Step 2: Match the options
Only pytest --html=report.html matches the correct syntax exactly.Final Answer:
pytest --html=report.html -> Option CQuick Check:
Use --html=filename.html for reports [OK]
- Using single dash instead of double dash
- Using incorrect option names like --report
- Missing equals sign between option and filename
pytest --html=results.html, what will happen after the tests finish?Solution
Step 1: Understand the command effect
The commandpytest --html=results.htmltells pytest to run tests and save results in an HTML file namedresults.html.Step 2: Identify the expected output
After tests finish, the HTML report file is created for easy viewing.Final Answer:
An HTML file named results.html will be created with test results -> Option BQuick Check:
--html=filename creates HTML report file [OK]
- Thinking no report is generated
- Expecting output only in console
- Assuming command syntax is wrong
pytest --html=report.html but no HTML report was created. What is the most likely cause?Solution
Step 1: Check plugin requirement
pytest-html must be installed for--htmloption to work.Step 2: Analyze other options
The command syntax is correct, tests can run without errors, and reports are not disabled by default.Final Answer:
pytest-html plugin is not installed -> Option AQuick Check:
Missing plugin causes no report [OK]
- Assuming command syntax is wrong
- Thinking reports are off by default
- Ignoring plugin installation requirement
Solution
Step 1: Understand pytest-html capabilities
pytest-html can include environment info if you add it via hooks in your test setup.Step 2: Evaluate options
Usepytest --html=report.html --self-contained-htmland add environment info via pytest hooks uses the correct command and mentions adding info via hooks, which is the recommended way. Runpytest --html=report.htmlonly; environment info is added automatically is incorrect because environment info is not automatic. Manually edit the HTML report after generation to add environment info is manual and error-prone. Usepytest --html=report.html --env-infooption is invalid; no such option exists.Final Answer:
Use pytest --html=report.html --self-contained-html and add environment info via pytest hooks -> Option AQuick Check:
Add env info via hooks + --html [OK]
- Expecting environment info added automatically
- Trying to edit HTML manually
- Using non-existent command options
Ready to prove it?
Pass the test and this pattern moves unknown
