Skip to main content

Quality Assurance

Elevating Selenium Testing: Comprehensive Reporting with Pytest

Close Up Of Businessman Using A Laptop With Graphs And Charts On A Laptop Computer.

When you’re running Selenium tests in Python, particularly in large projects, the ability to generate detailed and readable reports is essential for understanding test results, tracking failures, and improving overall test management. In this blog post, we’ll explore how to integrate reporting into your Selenium tests using Pytest, one of the most popular testing frameworks for Python.

Picture9

Why Do We Need Reporting?

Test reports provide more than just a summary of whether tests have passed or failed. They help teams:

  • Track which test cases are failing and why.
  • View test logs and tracebacks for deeper insights.
  • Generate aesthetically pleasing, understandable reports for stakeholders.
  • Integrate with continuous integration (CI) systems to display results in an easily consumable format. Report

By leveraging tools like pytest-html, allure-pytest, or pytest-splunk, you can generate rich and actionable test reports that greatly enhance the debugging process and provide clear feedback.

Setting Up Pytest for Selenium

Before we dive into reporting, let’s briefly set up Selenium with Pytest for a basic test case.

  1. Install Necessary Packages:

    bash
    pip install selenium pytest pytest-html
    

     

  2. Basic Selenium Test Example:

    from selenium import webdriver
    import pytest
    
    @pytest.fixture
    def driver():
        driver = webdriver.Chrome(executable_path="/path/to/chromedriver")
        yield driver
        driver.quit()
    
    def test_open_google(driver):
        driver.get("https://www.google.com")
        assert "Google" in driver.title

This basic test launches Chrome, navigates to Google, and asserts that the word “Google” appears in the page title.

Generating Reports with Pytest-HTML

One of the easiest ways to generate reports in Pytest is by using the pytest-html plugin. It produces clean, easy-to-read HTML reports after test execution. Here’s how to integrate it into your project.

  1. Install pytest-html:

    bash
    pip install pytest-html
    

     

  2. Running Tests with HTML Report: You can generate an HTML report by running Pytest with the –html option:
    Pytest with the --html option:
    
    bash
    pytest --html=report.html
    
  3. Report Output: After running the tests, the report.html file will be created in the current directory. Open the file in your browser to view a well-structured report that shows the status of each test (pass/fail), test duration, and logs.The report includes:
    • Test Summary: A section that lists the total number of tests, passed, failed, or skipped.
    • Test Case Details: Clickable details for each test case including the test name, outcome, duration, and logs for failed tests.
    • Screenshots: If you capture screenshots during your tests, they will be automatically added to the report (more on this later).


Adding Screenshots for Test Failures

One of the best practices in Selenium test automation is to capture screenshots when a test fails. This helps you quickly diagnose issues by giving you a visual reference.

Here’s how to capture screenshots in your Selenium tests and add them to your HTML report:

  1. Modify Test Code to Capture Screenshots on Failure:

    import pytest
    from selenium import webdriver
    import os
    from datetime import datetime
    
    @pytest.fixture
    def driver():
        driver = webdriver.Chrome(executable_path="/path/to/chromedriver")
        yield driver
        driver.quit()
    
    def take_screenshot(driver):
        timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
        screenshot_dir = "screenshots"
        os.makedirs(screenshot_dir, exist_ok=True)
        screenshot_path = f"{screenshot_dir}/screenshot_{timestamp}.png"
        driver.save_screenshot(screenshot_path)
        return screenshot_path
    
    def test_open_google(driver):
        driver.get("https://www.google.com")
        assert "NonexistentPage" in driver.title  # This will fail
    

     

  2. Update the Report to Include Screenshots: To ensure that the screenshot appears in the HTML report, modify your test setup and teardown functions to capture screenshots when the test fails.
    def pytest_runtest_makereport(item, call):
        if call.excinfo is not None:
            driver = item.funcargs.get("driver")
            screenshot_path = take_screenshot(driver)
            item.user_properties.append(("screenshot", screenshot_path))

    This will add a screenshot to the report whenever a test fails, making it much easier to troubleshoot issues.


Using Allure for Advanced Reporting

If you need more advanced reporting features, like interactive reports or integration with CI/CD pipelines, you can use Allure. Allure is a popular framework that provides rich, interactive test reports with a lot of customization options.

  1. Install Allure and Pytest Plugin:

    bash
    
    pip install allure-pytest

     

  2. Running Tests with Allure: To generate an allure report, run the following commands:
    bash
    
    pytest --alluredir=allure-results
    
    allure serve allure-results

    This will launch a local server with a beautiful, interactive report.


Conclusion

Integrating a robust reporting structure into your Selenium tests with Python (and Pytest) is a great way to gain deeper insights into your test results, improve test reliability, and speed up the debugging process. Whether you use pytest-html for quick and simple reports or opt for Allure for advanced, interactive reports, the ability to visualize your test outcomes is essential for maintaining high-quality automated test suites.

With reporting in place, you can more effectively track test progress, communicate results with your team, and ensure that issues are identified and resolved quickly.

Happy testing!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Ashika Meshram

Ashika Meshram is a Technical Consultant currently working on EHI. She is a coder girl fond of traveling and blogging.

More from this Author

Categories
Follow Us