0
# Content Extras
1
2
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.
3
4
## Capabilities
5
6
### Generic Extra Function
7
8
Creates a generic extra content item with custom format type and metadata.
9
10
```python { .api }
11
def extra(content, format_type, name=None, mime_type=None, extension=None):
12
"""
13
Create a generic extra content item.
14
15
Parameters:
16
- content (str): The content data
17
- format_type (str): Type of content (html, image, json, text, url, video)
18
- name (str, optional): Display name for the content
19
- mime_type (str, optional): MIME type for the content
20
- extension (str, optional): File extension for the content
21
22
Returns:
23
- dict: Extra content dictionary with keys: name, format_type, content, mime_type, extension
24
"""
25
```
26
27
### HTML Content
28
29
Embeds HTML content directly into the report.
30
31
```python { .api }
32
def html(content):
33
"""
34
Create HTML extra content.
35
36
Parameters:
37
- content (str): HTML content to embed
38
39
Returns:
40
- dict: HTML extra content item
41
"""
42
```
43
44
**Usage Example:**
45
46
```python
47
def test_with_html(extras):
48
html_content = '<div style="color: red;">Error occurred in step 3</div>'
49
extras.append(pytest_html.extras.html(html_content))
50
```
51
52
### Image Content
53
54
Embeds images in various formats into the report.
55
56
```python { .api }
57
def image(content, name="Image", mime_type="image/png", extension="png"):
58
"""
59
Create image extra content.
60
61
Parameters:
62
- content (str): Base64-encoded image data or image file path
63
- name (str): Display name for the image (default: "Image")
64
- mime_type (str): MIME type (default: "image/png")
65
- extension (str): File extension (default: "png")
66
67
Returns:
68
- dict: Image extra content item
69
"""
70
71
def png(content, name="Image"):
72
"""
73
Create PNG image extra content.
74
75
Parameters:
76
- content (str): Base64-encoded PNG data or PNG file path
77
- name (str): Display name for the image (default: "Image")
78
79
Returns:
80
- dict: PNG image extra content item with mime_type="image/png", extension="png"
81
"""
82
83
def jpg(content, name="Image"):
84
"""
85
Create JPEG image extra content.
86
87
Parameters:
88
- content (str): Base64-encoded JPEG data or JPEG file path
89
- name (str): Display name for the image (default: "Image")
90
91
Returns:
92
- dict: JPEG image extra content item with mime_type="image/jpeg", extension="jpg"
93
"""
94
95
def svg(content, name="Image"):
96
"""
97
Create SVG image extra content.
98
99
Parameters:
100
- content (str): SVG content or SVG file path
101
- name (str): Display name for the image (default: "Image")
102
103
Returns:
104
- dict: SVG image extra content item with mime_type="image/svg+xml", extension="svg"
105
"""
106
```
107
108
**Usage Examples:**
109
110
```python
111
import base64
112
113
def test_with_screenshot(extras):
114
# Add a PNG screenshot
115
with open("screenshot.png", "rb") as f:
116
screenshot_data = base64.b64encode(f.read()).decode()
117
extras.append(pytest_html.extras.png(screenshot_data, "Failed Test Screenshot"))
118
119
def test_with_chart(extras):
120
# Add an SVG chart
121
svg_chart = '<svg><circle cx="50" cy="50" r="40" fill="blue"/></svg>'
122
extras.append(pytest_html.extras.svg(svg_chart, "Performance Chart"))
123
```
124
125
### JSON Content
126
127
Embeds JSON data with syntax highlighting and formatting.
128
129
```python { .api }
130
def json(content, name="JSON"):
131
"""
132
Create JSON extra content.
133
134
Parameters:
135
- content (dict|list|str): JSON-serializable data or JSON string
136
- name (str): Display name for the JSON content (default: "JSON")
137
138
Returns:
139
- dict: JSON extra content item with mime_type="application/json", extension="json"
140
"""
141
```
142
143
**Usage Example:**
144
145
```python
146
def test_api_response(extras):
147
response_data = {
148
"status": "error",
149
"code": 404,
150
"message": "Resource not found",
151
"timestamp": "2023-01-01T12:00:00Z"
152
}
153
extras.append(pytest_html.extras.json(response_data, "API Response"))
154
```
155
156
### Text Content
157
158
Embeds plain text content with proper formatting.
159
160
```python { .api }
161
def text(content, name="Text"):
162
"""
163
Create text extra content.
164
165
Parameters:
166
- content (str): Plain text content
167
- name (str): Display name for the text content (default: "Text")
168
169
Returns:
170
- dict: Text extra content item with mime_type="text/plain", extension="txt"
171
"""
172
```
173
174
**Usage Example:**
175
176
```python
177
def test_with_logs(extras):
178
log_content = """
179
2023-01-01 12:00:00 INFO Starting test execution
180
2023-01-01 12:00:01 DEBUG Database connection established
181
2023-01-01 12:00:02 ERROR Failed to authenticate user
182
2023-01-01 12:00:03 INFO Test completed with errors
183
"""
184
extras.append(pytest_html.extras.text(log_content.strip(), "Test Logs"))
185
```
186
187
### URL Links
188
189
Adds external links to the report.
190
191
```python { .api }
192
def url(content, name="URL"):
193
"""
194
Create URL link extra content.
195
196
Parameters:
197
- content (str): URL to link to
198
- name (str): Display name for the link (default: "URL")
199
200
Returns:
201
- dict: URL extra content item
202
"""
203
```
204
205
**Usage Example:**
206
207
```python
208
def test_external_service(extras):
209
extras.append(pytest_html.extras.url("https://api.example.com/status", "Service Status"))
210
extras.append(pytest_html.extras.url("https://docs.example.com/api", "API Documentation"))
211
```
212
213
### Video Content
214
215
Embeds video content into the report.
216
217
```python { .api }
218
def video(content, name="Video", mime_type="video/mp4", extension="mp4"):
219
"""
220
Create video extra content.
221
222
Parameters:
223
- content (str): Base64-encoded video data or video file path
224
- name (str): Display name for the video (default: "Video")
225
- mime_type (str): MIME type (default: "video/mp4")
226
- extension (str): File extension (default: "mp4")
227
228
Returns:
229
- dict: Video extra content item
230
"""
231
232
def mp4(content, name="Video"):
233
"""
234
Create MP4 video extra content.
235
236
Parameters:
237
- content (str): Base64-encoded MP4 data or MP4 file path
238
- name (str): Display name for the video (default: "Video")
239
240
Returns:
241
- dict: MP4 video extra content item
242
"""
243
```
244
245
**Usage Example:**
246
247
```python
248
def test_with_recording(extras):
249
# Add a screen recording of the test
250
with open("test_recording.mp4", "rb") as f:
251
video_data = base64.b64encode(f.read()).decode()
252
extras.append(pytest_html.extras.mp4(video_data, "Test Recording"))
253
```
254
255
## Format Type Constants
256
257
```python { .api }
258
FORMAT_HTML = "html"
259
FORMAT_IMAGE = "image"
260
FORMAT_JSON = "json"
261
FORMAT_TEXT = "text"
262
FORMAT_URL = "url"
263
FORMAT_VIDEO = "video"
264
```
265
266
These constants define the supported content types for extras and are used internally by the extra creation functions.
267
268
## Content Processing
269
270
The HTML report generator processes different content types as follows:
271
272
- **HTML**: Embedded directly into the report
273
- **Images**: Displayed as inline images or saved as asset files
274
- **JSON**: Formatted with syntax highlighting and collapsible structure
275
- **Text**: Displayed in preformatted text blocks
276
- **URLs**: Rendered as clickable links
277
- **Videos**: Embedded as playable video elements
278
279
Content can be provided as:
280
- **Base64-encoded data**: For binary content like images and videos
281
- **File paths**: Referenced files that will be copied to report assets
282
- **Direct content**: For text-based formats like HTML, JSON, and text