0
# Steps and Attachments
1
2
Enhance test reporting with step-by-step execution tracking and rich attachments. Steps break down test execution into logical phases while attachments provide supporting evidence like screenshots, logs, and data files.
3
4
## Capabilities
5
6
### Test Steps
7
8
Create hierarchical test steps to document test execution flow and make debugging easier by identifying exactly where failures occur.
9
10
```python { .api }
11
@allure.step(title: str)
12
"""
13
Decorator to mark a function as a test step.
14
15
Parameters:
16
- title: Step title (supports parameter formatting with {})
17
18
Usage:
19
@allure.step("Login with username {username}")
20
def login(username, password):
21
pass
22
"""
23
24
allure.step(title: str)
25
"""
26
Context manager for creating test steps.
27
28
Parameters:
29
- title: Step title for the step block
30
31
Usage:
32
with allure.step("Navigate to login page"):
33
driver.get("/login")
34
"""
35
```
36
37
### File Attachments
38
39
Attach external files like screenshots, logs, configuration files, and test data to test results.
40
41
```python { .api }
42
allure.attach.file(source: str, name: str = None, attachment_type: str = None, extension: str = None)
43
"""
44
Attach an external file to the test result.
45
46
Parameters:
47
- source: Path to the file to attach
48
- name: Display name for the attachment (optional)
49
- attachment_type: MIME type of the file (optional, auto-detected if not provided)
50
- extension: File extension (optional, auto-detected if not provided)
51
52
Usage:
53
allure.attach.file("./logs/test.log", name="Test Log", attachment_type="text/plain")
54
allure.attach.file("./screenshots/error.png", name="Error Screenshot")
55
"""
56
```
57
58
### Data Attachments
59
60
Attach in-memory data like API responses, database queries, or generated content directly to test results.
61
62
```python { .api }
63
allure.attach(body: Any, name: str = None, attachment_type: str = None, extension: str = None)
64
"""
65
Attach data directly to the test result.
66
67
Parameters:
68
- body: Data to attach (string, bytes, or other serializable data)
69
- name: Display name for the attachment (optional)
70
- attachment_type: MIME type of the data (optional)
71
- extension: File extension for the attachment (optional)
72
73
Usage:
74
allure.attach(json.dumps(response), name="API Response", attachment_type="application/json")
75
allure.attach(screenshot_bytes, name="Screenshot", attachment_type="image/png")
76
"""
77
```
78
79
## Usage Examples
80
81
### Basic Step Tracking
82
83
```python
84
import allure
85
86
def test_user_registration():
87
with allure.step("Navigate to registration page"):
88
driver.get("/register")
89
90
with allure.step("Fill registration form"):
91
driver.find_element("name", "username").send_keys("testuser")
92
driver.find_element("name", "email").send_keys("test@example.com")
93
driver.find_element("name", "password").send_keys("password123")
94
95
with allure.step("Submit registration"):
96
driver.find_element("type", "submit").click()
97
98
with allure.step("Verify successful registration"):
99
success_message = driver.find_element("class", "success").text
100
assert "Registration successful" in success_message
101
```
102
103
### Step Decorator with Parameters
104
105
```python
106
import allure
107
108
@allure.step("Login with username '{username}'")
109
def login_user(username, password):
110
driver.find_element("name", "username").send_keys(username)
111
driver.find_element("name", "password").send_keys(password)
112
driver.find_element("type", "submit").click()
113
114
@allure.step("Navigate to page '{page_name}'")
115
def navigate_to_page(page_name, url):
116
driver.get(url)
117
118
def test_user_workflow():
119
login_user("testuser", "password123")
120
navigate_to_page("Dashboard", "/dashboard")
121
```
122
123
### Comprehensive Attachments
124
125
```python
126
import allure
127
import json
128
import requests
129
130
def test_api_integration():
131
with allure.step("Send API request"):
132
response = requests.get("https://api.example.com/users")
133
134
# Attach request details
135
allure.attach(
136
f"GET {response.url}\nStatus: {response.status_code}",
137
name="Request Details",
138
attachment_type="text/plain"
139
)
140
141
# Attach response data
142
allure.attach(
143
json.dumps(response.json(), indent=2),
144
name="API Response",
145
attachment_type="application/json"
146
)
147
148
with allure.step("Take screenshot of results"):
149
screenshot = driver.get_screenshot_as_png()
150
allure.attach(
151
screenshot,
152
name="API Results Screenshot",
153
attachment_type="image/png"
154
)
155
156
with allure.step("Save test log"):
157
allure.attach.file(
158
"./logs/api_test.log",
159
name="Test Execution Log",
160
attachment_type="text/plain"
161
)
162
```
163
164
### Nested Steps
165
166
```python
167
import allure
168
169
@allure.step("Setup test environment")
170
def setup_environment():
171
with allure.step("Start application server"):
172
# Start server logic
173
pass
174
175
with allure.step("Initialize database"):
176
# Database setup logic
177
pass
178
179
with allure.step("Configure test data"):
180
# Test data setup logic
181
pass
182
183
def test_complex_workflow():
184
setup_environment()
185
186
with allure.step("Execute main test logic"):
187
with allure.step("Perform user actions"):
188
# Test actions
189
pass
190
191
with allure.step("Validate results"):
192
# Assertions
193
pass
194
```
195
196
## Attachment Best Practices
197
198
- **Screenshots**: Always attach screenshots on test failures for UI tests
199
- **Logs**: Include relevant log files for debugging failed tests
200
- **API Data**: Attach request/response data for API tests
201
- **Configuration**: Include configuration files when environment issues occur
202
- **Test Data**: Attach input data files when data-driven tests fail
203
- **Network Traces**: Include network captures for integration test failures
204
205
The attachments appear in the Allure report and provide essential context for understanding test execution and debugging failures.