Bird
Raised Fist0
PyTesttesting~10 mins

pytest-html for HTML reports - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the pytest-html plugin.

PyTest
import pytest

@pytest.mark.usefixtures([1])
def test_example():
    assert 1 == 1
Drag options to blanks, or click blank then click option'
Apytest_html
Bhtml
Cpytest_html_report
Dhtml_report
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect fixture names like 'html' or 'html_report' which do not exist.
2fill in blank
medium

Complete the command to generate an HTML report using pytest-html.

PyTest
pytest --[1]=report.html
Drag options to blanks, or click blank then click option'
Ahtmlpath
Bhtml
Chtml-report
Dhtmlfile
Attempts:
3 left
💡 Hint
Common Mistakes
Using options like '--htmlpath' or '--htmlfile' which are not recognized by pytest-html.
3fill in blank
hard

Fix the error in the pytest command to include extra metadata in the HTML report.

PyTest
pytest --html=report.html --[1]='Project: Demo'
Drag options to blanks, or click blank then click option'
Ametadata
Bextra
Cmetadata-add
Dself-contained-html
Attempts:
3 left
💡 Hint
Common Mistakes
Using '--extra' or '--metadata-add' which are not valid pytest-html options.
4fill in blank
hard

Fill both blanks to add a hook function that modifies the HTML report title and environment info.

PyTest
def pytest_html_report_title(report):
    report.title = [1]

def pytest_html_environment(config):
    return [2]
Drag options to blanks, or click blank then click option'
A"My Test Report"
B{"Python": "3.12", "Platform": "Linux"}
C"Test Report"
D{"OS": "Windows", "Browser": "Chrome"}
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect data types like passing a dictionary as title or string as environment info.
5fill in blank
hard

Fill all three blanks to add a pytest hook that appends extra info to the HTML report.

PyTest
def pytest_html_results_table_row(report, cells):
    if report.failed:
        cells.append([1])

@pytest.hookimpl(optionalhook=True)
def pytest_html_results_table_header(cells):
    cells.append([2])

@pytest.hookimpl(optionalhook=True)
def pytest_html_results_table_html(report, data):
    if report.failed:
        data.append([3])
Drag options to blanks, or click blank then click option'
Ahtml.th('Extra Info')
Bhtml.td('Extra Info')
Chtml.div('Failure details')
Dhtml.td('Failure details')
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up td and th tags or using incorrect HTML elements.

Practice

(1/5)
1. What is the main purpose of using pytest-html in testing?
easy
A. To run tests faster than usual
B. To convert Python code into HTML
C. To write tests in HTML format
D. To generate easy-to-read HTML reports of test results

Solution

  1. Step 1: Understand pytest-html functionality

    pytest-html is a plugin that creates HTML reports summarizing test results.
  2. Step 2: Identify the main use

    The main use is to generate readable reports, not to speed up tests or convert code.
  3. Final Answer:

    To generate easy-to-read HTML reports of test results -> Option D
  4. Quick Check:

    pytest-html = HTML reports [OK]
Hint: Remember: pytest-html = HTML report generator [OK]
Common Mistakes:
  • Thinking pytest-html speeds up tests
  • Confusing report generation with test execution
  • Assuming tests are written in HTML
2. Which command correctly generates an HTML report named report.html using pytest?
easy
A. pytest --report=report.html
B. pytest -html report.html
C. pytest --html=report.html
D. pytest -r html=report.html

Solution

  1. Step 1: Recall pytest-html command syntax

    The correct syntax to generate an HTML report is pytest --html=filename.html.
  2. Step 2: Match the options

    Only pytest --html=report.html matches the correct syntax exactly.
  3. Final Answer:

    pytest --html=report.html -> Option C
  4. Quick Check:

    Use --html=filename.html for reports [OK]
Hint: Use double dash and equals for HTML report file [OK]
Common Mistakes:
  • Using single dash instead of double dash
  • Using incorrect option names like --report
  • Missing equals sign between option and filename
3. Given this pytest command: pytest --html=results.html, what will happen after the tests finish?
medium
A. Tests will run and output will be printed only in the console
B. An HTML file named results.html will be created with test results
C. Tests will run but no report will be generated
D. An error will occur because the command is invalid

Solution

  1. Step 1: Understand the command effect

    The command pytest --html=results.html tells pytest to run tests and save results in an HTML file named results.html.
  2. Step 2: Identify the expected output

    After tests finish, the HTML report file is created for easy viewing.
  3. Final Answer:

    An HTML file named results.html will be created with test results -> Option B
  4. Quick Check:

    --html=filename creates HTML report file [OK]
Hint: --html=filename always creates HTML report file [OK]
Common Mistakes:
  • Thinking no report is generated
  • Expecting output only in console
  • Assuming command syntax is wrong
4. You ran pytest --html=report.html but no HTML report was created. What is the most likely cause?
medium
A. pytest-html plugin is not installed
B. The command syntax is incorrect
C. Tests did not run due to syntax errors
D. HTML reports are disabled by default

Solution

  1. Step 1: Check plugin requirement

    pytest-html must be installed for --html option to work.
  2. Step 2: Analyze other options

    The command syntax is correct, tests can run without errors, and reports are not disabled by default.
  3. Final Answer:

    pytest-html plugin is not installed -> Option A
  4. Quick Check:

    Missing plugin causes no report [OK]
Hint: Install pytest-html plugin to enable reports [OK]
Common Mistakes:
  • Assuming command syntax is wrong
  • Thinking reports are off by default
  • Ignoring plugin installation requirement
5. You want to generate an HTML report with pytest that includes extra environment info like Python version and OS. Which approach is best?
hard
A. Use pytest --html=report.html --self-contained-html and add environment info via pytest hooks
B. Run pytest --html=report.html only; environment info is added automatically
C. Manually edit the HTML report after generation to add environment info
D. Use pytest --html=report.html --env-info option

Solution

  1. Step 1: Understand pytest-html capabilities

    pytest-html can include environment info if you add it via hooks in your test setup.
  2. Step 2: Evaluate options

    Use pytest --html=report.html --self-contained-html and add environment info via pytest hooks uses the correct command and mentions adding info via hooks, which is the recommended way. Run pytest --html=report.html only; 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. Use pytest --html=report.html --env-info option is invalid; no such option exists.
  3. Final Answer:

    Use pytest --html=report.html --self-contained-html and add environment info via pytest hooks -> Option A
  4. Quick Check:

    Add env info via hooks + --html [OK]
Hint: Add environment info via hooks, not command options [OK]
Common Mistakes:
  • Expecting environment info added automatically
  • Trying to edit HTML manually
  • Using non-existent command options
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard