How I can update the environment table in pytest-html

2 min read 06-10-2024
How I can update the environment table in pytest-html


Enhancing Your Pytest Reports: Customizing the Environment Table in pytest-html

pytest-html is a powerful plugin for pytest that generates beautiful HTML reports of your test runs. While the default report is comprehensive, sometimes you need more specific information about the environment your tests ran in. This is where customizing the environment table comes in.

The Problem: You want to include additional, relevant data in the environment table of your pytest-html report, like specific versions of libraries, configuration settings, or even custom environment variables.

The Solution: This article will guide you through the process of updating the environment table to include the information you need.

Scenario:

Let's say you have a project where you want to include the current version of a custom library you developed called "my_library" in your pytest-html report.

Original Code:

import pytest
import my_library

@pytest.fixture
def environment_info():
    return {
        "python_version": platform.python_version(),
        # ... other environment variables
    }

@pytest.mark.parametrize("test_data", [1, 2, 3])
def test_my_library(test_data, environment_info):
    # Your test logic goes here
    my_library.do_something(test_data)

    # Add the custom library version to the environment information
    environment_info["my_library_version"] = my_library.__version__

    # ... other assertions and test logic

Analysis and Clarification:

In this code, we've created a environment_info fixture that captures some basic environment information. However, the my_library_version is added after the test execution, which means it won't be present in the environment table of the report.

Solution:

To include this custom information, you can leverage pytest-html's extra_env_vars parameter.

Here's how you can update the code:

import pytest
import my_library

@pytest.fixture
def environment_info():
    return {
        "python_version": platform.python_version(),
        # ... other environment variables
    }

@pytest.mark.parametrize("test_data", [1, 2, 3])
def test_my_library(test_data, environment_info):
    # Your test logic goes here
    my_library.do_something(test_data)

    # ... other assertions and test logic
    environment_info["my_library_version"] = my_library.__version__

    return environment_info

# This hook function allows you to modify the environment information
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_html_report_title(report):
    outcome = yield
    report.extra_env_vars.append(
        (
            "my_library_version",
            report.user_properties.get("environment_info", {}).get("my_library_version"),
        )
    )

Explanation:

  1. We modify the test_my_library function to return the updated environment_info dictionary.
  2. We implement a hook function, pytest_html_report_title, which allows us to modify the report before it's finalized.
  3. Inside the hook, we use the report.extra_env_vars attribute to add a new entry to the environment table with the "my_library_version" key and its value.

Further Enhancements:

  • You can extend the environment table with other custom information like the database version, the operating system, or even custom environment variables.
  • Use the report.user_properties attribute to store other relevant information that you want to include in the report.

Conclusion:

By utilizing pytest-html's hooks and the extra_env_vars parameter, you can tailor the environment table to your needs, providing valuable insights about your testing environment within the HTML report.

References:

By customizing your pytest-html reports, you can gain a deeper understanding of your test runs and ensure that your environment is properly documented.