Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Elevating Selenium Testing: Comprehensive Reporting with Pytest

    Elevating Selenium Testing: Comprehensive Reporting with Pytest

    December 20, 2024

    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!

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleUnderstanding Lifecycle Methods in Vue.js
    Next Article Universal Design for Visual Disabilities in Healthcare – Addressing Color Vision Deficiency – 8

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 17, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2024-47893 – VMware GPU Firmware Memory Disclosure

    May 17, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Build an automated insight extraction framework for customer feedback analysis with Amazon Bedrock and Amazon QuickSight

    Development

    Malicious Google Ads Pushing Fake IP Scanner Software with Hidden Backdoor

    Development

    Window Maker Live – installable Linux live distro

    Development

    DeepSeek: Beyond the Hype—A Game Changer

    Web Development

    Highlights

    CVE-2025-46521 – Silver Muru WS Force Stored Cross-site Scripting

    April 24, 2025

    CVE ID : CVE-2025-46521

    Published : April 24, 2025, 4:15 p.m. | 2 hours, 44 minutes ago

    Description : Improper Neutralization of Input During Web Page Generation (‘Cross-site Scripting’) vulnerability in Silver Muru WS Force Login Page allows Stored XSS. This issue affects WS Force Login Page: from n/a through 3.0.3.

    Severity: 5.9 | MEDIUM

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    How I used this portable, ink-free printer to declutter my workstation (and it’s 60% off)

    February 20, 2025

    Diablo 4 “Berserk” anime collaboration revealed in full — New info on themed cosmetics, items, and a launch date

    April 22, 2025

    Rilasciato LibreOffice 25.2

    February 7, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.