Bird
Raised Fist0
Selenium Pythontesting~8 mins

HTML report generation in Selenium Python - Framework Patterns

Choose your learning style10 modes available
Framework Mode - HTML report generation
Folder Structure
test_project/
├── tests/
│   ├── test_login.py
│   ├── test_checkout.py
│   └── __init__.py
├── pages/
│   ├── login_page.py
│   ├── checkout_page.py
│   └── __init__.py
├── reports/
│   └── (generated HTML reports here)
├── utils/
│   ├── driver_factory.py
│   ├── logger.py
│   └── __init__.py
├── config/
│   ├── config.yaml
│   └── __init__.py
├── conftest.py
└── pytest.ini
Test Framework Layers
  • Driver Layer: Handles browser setup and teardown (e.g., driver_factory.py).
  • Page Objects: Encapsulate page elements and actions (e.g., login_page.py).
  • Tests: Test cases using PyTest that call page objects and assert results (e.g., test_login.py).
  • Utilities: Helper functions like logging and report generation support.
  • Configuration: Environment settings, browser options, credentials stored in config.yaml.
  • Reports: HTML reports generated after test runs stored in reports/ folder.
Configuration Patterns
  • Environment Settings: Use config/config.yaml to define URLs, browsers, and credentials for different environments (dev, staging, prod).
  • Browser Selection: Pass browser choice via command line or config file, used by driver_factory.py to launch correct browser.
  • Credentials: Store securely in config file or environment variables, accessed in tests or page objects.
  • PyTest ini: Use pytest.ini to configure test runs and plugins like pytest-html for report generation.
Test Reporting and CI/CD Integration
  • HTML Reports: Use pytest-html plugin to generate detailed HTML reports after test execution.
  • Report Location: Reports saved in reports/ folder with timestamped filenames for history.
  • CI/CD Integration: Configure CI pipelines (e.g., GitHub Actions, Jenkins) to run tests and archive HTML reports as artifacts.
  • Notifications: Optionally send report links or summaries via email or messaging tools after CI runs.
Best Practices for HTML Report Generation Framework
  1. Separate Reports Folder: Keep all reports in a dedicated folder to avoid clutter and ease access.
  2. Use Plugins: Leverage existing tools like pytest-html for reliable and rich HTML reports.
  3. Timestamp Reports: Include timestamps in report filenames to track test history and avoid overwriting.
  4. Integrate with CI: Automate report generation and storage in CI pipelines for continuous feedback.
  5. Readable Reports: Ensure reports include clear test names, statuses, screenshots on failure, and logs for easy debugging.
Self Check

Where in this folder structure would you add a new utility to capture screenshots on test failure for the HTML report?

Key Result
Organize Selenium Python tests with clear layers and use pytest-html plugin to generate and store HTML reports in a dedicated folder.

Practice

(1/5)
1. What is the main purpose of generating an HTML report in Selenium Python testing?
easy
A. To avoid writing assertions
B. To speed up test execution
C. To make test results easy to read and share
D. To write test cases faster

Solution

  1. Step 1: Understand the role of HTML reports

    HTML reports present test results in a clear, visual format that anyone can read.
  2. Step 2: Compare with other options

    Speeding tests, writing tests faster, or avoiding assertions are unrelated to HTML reports.
  3. Final Answer:

    To make test results easy to read and share -> Option C
  4. Quick Check:

    HTML reports = clear, shareable results [OK]
Hint: HTML reports show results clearly for sharing [OK]
Common Mistakes:
  • Thinking HTML reports speed up tests
  • Confusing report generation with test writing
  • Believing reports remove need for assertions
2. Which Python package is commonly used with unittest to generate HTML reports in Selenium tests?
easy
A. pytest-html
B. unittest-html
C. selenium-html
D. HtmlTestRunner

Solution

  1. Step 1: Identify the package for unittest HTML reports

    HtmlTestRunner is designed to work with unittest for HTML report generation.
  2. Step 2: Eliminate other options

    pytest-html is for pytest, selenium-html and unittest-html are not standard packages.
  3. Final Answer:

    HtmlTestRunner -> Option D
  4. Quick Check:

    unittest + HtmlTestRunner = HTML reports [OK]
Hint: HtmlTestRunner works with unittest for HTML reports [OK]
Common Mistakes:
  • Choosing pytest-html for unittest tests
  • Assuming selenium-html is a real package
  • Confusing unittest-html as official
3. Given this code snippet using HtmlTestRunner, what folder will the HTML report be saved in?
import unittest
import HtmlTestRunner

class TestExample(unittest.TestCase):
    def test_pass(self):
        self.assertTrue(True)

if __name__ == '__main__':
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='reports'))
medium
A. Current directory
B. reports folder
C. HtmlTestRunner folder
D. No folder, prints to console only

Solution

  1. Step 1: Check the output parameter in HTMLTestRunner

    The output='reports' argument tells HtmlTestRunner to save reports in the 'reports' folder.
  2. Step 2: Understand default behavior

    Without output specified, reports save in current directory, but here 'reports' is explicit.
  3. Final Answer:

    reports folder -> Option B
  4. Quick Check:

    output='reports' means save in reports folder [OK]
Hint: output='foldername' sets report save location [OK]
Common Mistakes:
  • Assuming reports save in current directory by default
  • Thinking HtmlTestRunner creates its own folder
  • Believing reports print only to console
4. What is wrong with this code snippet for generating an HTML report using HtmlTestRunner?
import unittest
import HtmlTestRunner

class TestExample(unittest.TestCase):
    def test_fail(self):
        self.assertEqual(1, 2)

if __name__ == '__main__':
    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output=''))
medium
A. Empty output string causes report not to save
B. assertEqual syntax is incorrect
C. Missing import for HtmlTestRunner
D. Test class must inherit from HtmlTestRunner

Solution

  1. Step 1: Check the output parameter usage

    Setting output='' means no folder is specified, which can cause HtmlTestRunner to fail saving reports properly.
  2. Step 2: Verify other parts

    assertEqual syntax is correct, HtmlTestRunner is imported, and test class inheritance is correct.
  3. Final Answer:

    Empty output string causes report not to save -> Option A
  4. Quick Check:

    output='' disables report folder, causing save issues [OK]
Hint: Provide a valid folder name in output to save reports [OK]
Common Mistakes:
  • Thinking assertEqual(1, 2) is syntax error
  • Believing test class must inherit HtmlTestRunner
  • Ignoring output parameter effect
5. You want to generate an HTML report that shows detailed pass/fail results and saves in a folder named 'test_reports'. Which code snippet correctly sets this up using HtmlTestRunner with unittest?
hard
A. unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='test_reports'))
B. unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(report='test_reports'))
C. unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(folder='test_reports'))
D. unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(path='test_reports'))

Solution

  1. Step 1: Identify correct parameter for output folder

    HtmlTestRunner uses 'output' parameter to specify the folder for saving reports.
  2. Step 2: Check other parameter names

    'report', 'folder', and 'path' are not valid parameters for HtmlTestRunner's HTMLTestRunner class.
  3. Final Answer:

    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='test_reports')) -> Option A
  4. Quick Check:

    Use output='foldername' to save reports correctly [OK]
Hint: Use output='foldername' to set report save location [OK]
Common Mistakes:
  • Using wrong parameter names like report or folder
  • Not specifying output folder at all
  • Confusing HtmlTestRunner options with other packages
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