A pytest plugin for generating HTML reports of test results with enhanced visualizations and extensibility.
—
Functions for creating rich multimedia content that can be embedded in HTML test reports. These extras allow tests to include images, videos, JSON data, external links, and other content types to provide additional context and debugging information.
Creates a generic extra content item with custom format type and metadata.
def extra(content, format_type, name=None, mime_type=None, extension=None):
"""
Create a generic extra content item.
Parameters:
- content (str): The content data
- format_type (str): Type of content (html, image, json, text, url, video)
- name (str, optional): Display name for the content
- mime_type (str, optional): MIME type for the content
- extension (str, optional): File extension for the content
Returns:
- dict: Extra content dictionary with keys: name, format_type, content, mime_type, extension
"""Embeds HTML content directly into the report.
def html(content):
"""
Create HTML extra content.
Parameters:
- content (str): HTML content to embed
Returns:
- dict: HTML extra content item
"""Usage Example:
def test_with_html(extras):
html_content = '<div style="color: red;">Error occurred in step 3</div>'
extras.append(pytest_html.extras.html(html_content))Embeds images in various formats into the report.
def image(content, name="Image", mime_type="image/png", extension="png"):
"""
Create image extra content.
Parameters:
- content (str): Base64-encoded image data or image file path
- name (str): Display name for the image (default: "Image")
- mime_type (str): MIME type (default: "image/png")
- extension (str): File extension (default: "png")
Returns:
- dict: Image extra content item
"""
def png(content, name="Image"):
"""
Create PNG image extra content.
Parameters:
- content (str): Base64-encoded PNG data or PNG file path
- name (str): Display name for the image (default: "Image")
Returns:
- dict: PNG image extra content item with mime_type="image/png", extension="png"
"""
def jpg(content, name="Image"):
"""
Create JPEG image extra content.
Parameters:
- content (str): Base64-encoded JPEG data or JPEG file path
- name (str): Display name for the image (default: "Image")
Returns:
- dict: JPEG image extra content item with mime_type="image/jpeg", extension="jpg"
"""
def svg(content, name="Image"):
"""
Create SVG image extra content.
Parameters:
- content (str): SVG content or SVG file path
- name (str): Display name for the image (default: "Image")
Returns:
- dict: SVG image extra content item with mime_type="image/svg+xml", extension="svg"
"""Usage Examples:
import base64
def test_with_screenshot(extras):
# Add a PNG screenshot
with open("screenshot.png", "rb") as f:
screenshot_data = base64.b64encode(f.read()).decode()
extras.append(pytest_html.extras.png(screenshot_data, "Failed Test Screenshot"))
def test_with_chart(extras):
# Add an SVG chart
svg_chart = '<svg><circle cx="50" cy="50" r="40" fill="blue"/></svg>'
extras.append(pytest_html.extras.svg(svg_chart, "Performance Chart"))Embeds JSON data with syntax highlighting and formatting.
def json(content, name="JSON"):
"""
Create JSON extra content.
Parameters:
- content (dict|list|str): JSON-serializable data or JSON string
- name (str): Display name for the JSON content (default: "JSON")
Returns:
- dict: JSON extra content item with mime_type="application/json", extension="json"
"""Usage Example:
def test_api_response(extras):
response_data = {
"status": "error",
"code": 404,
"message": "Resource not found",
"timestamp": "2023-01-01T12:00:00Z"
}
extras.append(pytest_html.extras.json(response_data, "API Response"))Embeds plain text content with proper formatting.
def text(content, name="Text"):
"""
Create text extra content.
Parameters:
- content (str): Plain text content
- name (str): Display name for the text content (default: "Text")
Returns:
- dict: Text extra content item with mime_type="text/plain", extension="txt"
"""Usage Example:
def test_with_logs(extras):
log_content = """
2023-01-01 12:00:00 INFO Starting test execution
2023-01-01 12:00:01 DEBUG Database connection established
2023-01-01 12:00:02 ERROR Failed to authenticate user
2023-01-01 12:00:03 INFO Test completed with errors
"""
extras.append(pytest_html.extras.text(log_content.strip(), "Test Logs"))Adds external links to the report.
def url(content, name="URL"):
"""
Create URL link extra content.
Parameters:
- content (str): URL to link to
- name (str): Display name for the link (default: "URL")
Returns:
- dict: URL extra content item
"""Usage Example:
def test_external_service(extras):
extras.append(pytest_html.extras.url("https://api.example.com/status", "Service Status"))
extras.append(pytest_html.extras.url("https://docs.example.com/api", "API Documentation"))Embeds video content into the report.
def video(content, name="Video", mime_type="video/mp4", extension="mp4"):
"""
Create video extra content.
Parameters:
- content (str): Base64-encoded video data or video file path
- name (str): Display name for the video (default: "Video")
- mime_type (str): MIME type (default: "video/mp4")
- extension (str): File extension (default: "mp4")
Returns:
- dict: Video extra content item
"""
def mp4(content, name="Video"):
"""
Create MP4 video extra content.
Parameters:
- content (str): Base64-encoded MP4 data or MP4 file path
- name (str): Display name for the video (default: "Video")
Returns:
- dict: MP4 video extra content item
"""Usage Example:
def test_with_recording(extras):
# Add a screen recording of the test
with open("test_recording.mp4", "rb") as f:
video_data = base64.b64encode(f.read()).decode()
extras.append(pytest_html.extras.mp4(video_data, "Test Recording"))FORMAT_HTML = "html"
FORMAT_IMAGE = "image"
FORMAT_JSON = "json"
FORMAT_TEXT = "text"
FORMAT_URL = "url"
FORMAT_VIDEO = "video"These constants define the supported content types for extras and are used internally by the extra creation functions.
The HTML report generator processes different content types as follows:
Content can be provided as:
Install with Tessl CLI
npx tessl i tessl/pypi-pytest-html