0
0
PyTesttesting~15 mins

pytest-html for HTML reports - Build an Automation Script

Choose your learning style9 modes available
Generate HTML report using pytest-html plugin
Preconditions (2)
Step 1: Run pytest command with --html=report.html option
Step 2: Wait for tests to complete
Step 3: Open the generated report.html file in a browser
✅ Expected Result: The HTML report file is created with test results summary and details visible in the browser
Automation Requirements - pytest
Assertions Needed:
Verify the pytest command runs successfully
Verify the report.html file is created
Verify the report.html file contains the test name and pass/fail status
Best Practices:
Use pytest fixtures for setup and teardown if needed
Use subprocess module to run pytest command in automation script
Check file existence and content with assertions
Automated Solution
PyTest
import subprocess
import os
import re

def test_generate_html_report():
    # Run pytest with html report option
    result = subprocess.run(['pytest', '--html=report.html', '--self-contained-html'], capture_output=True, text=True)
    
    # Assert pytest ran successfully
    assert result.returncode == 0, f"Pytest failed with output:\n{result.stdout}\n{result.stderr}"

    # Assert report.html file exists
    assert os.path.exists('report.html'), "HTML report file was not created"

    # Read report content
    with open('report.html', 'r', encoding='utf-8') as f:
        content = f.read()

    # Check that test function name appears in report
    assert re.search(r'test_generate_html_report', content), "Test name not found in HTML report"

    # Check that status 'passed' or 'failed' appears
    assert re.search(r'passed|failed', content, re.IGNORECASE), "Test status not found in HTML report"

This script runs pytest with the --html=report.html option to generate an HTML report.

We use subprocess.run to execute the pytest command and capture its output and return code.

We assert that pytest finishes successfully by checking returncode == 0.

Then we check if the report.html file was created.

Finally, we open the report file and verify it contains the test function name and a test status like 'passed' or 'failed'.

This ensures the HTML report was generated correctly and includes test results.

Common Mistakes - 4 Pitfalls
{'mistake': 'Not installing pytest-html plugin before running tests', 'why_bad': 'The pytest command will fail or ignore the --html option, so no report is generated.', 'correct_approach': "Install pytest-html using 'pip install pytest-html' before running tests."}
{'mistake': 'Running pytest without capturing output or checking return code', 'why_bad': "You won't know if pytest failed or passed, making the automation unreliable.", 'correct_approach': 'Use subprocess.run with capture_output=True and check returncode to verify pytest success.'}
Checking for report file existence without verifying content
Hardcoding file paths without considering working directory
Bonus Challenge

Now add data-driven testing with 3 different simple test functions and generate a combined HTML report

Show Hint