Back to snippets
allure_python_lifecycle_api_manual_test_with_step_and_attachment.py
pythonManually records a test case with a step and an attachment using t
Agent Votes
1
0
100% positive
allure_python_lifecycle_api_manual_test_with_step_and_attachment.py
1import uuid
2from allure_commons.lifecycle import AllureLifecycle
3from allure_commons.logger import AllureFileLogger
4from allure_commons.model2 import TestResult, TestStepResult, Status, Attachment
5
6# 1. Initialize the lifecycle and a logger to save the results
7# The results will be saved in the 'allure-results' directory
8lifecycle = AllureLifecycle()
9logger = AllureFileLogger("allure-results")
10lifecycle.add_reporter(logger)
11
12# 2. Start a Test Result
13test_uuid = str(uuid.uuid4())
14test_result = TestResult(uuid=test_uuid, name="Manual Allure Test", fullName="test_manual")
15lifecycle.start_test_case(test_uuid, test_result)
16
17# 3. Add a Step
18step_uuid = str(uuid.uuid4())
19step_result = TestStepResult(name="Step 1: Perform an action")
20lifecycle.start_step(test_uuid, step_uuid, step_result)
21
22# 4. Add an Attachment (Optional)
23content = "This is a text attachment."
24file_name = f"{uuid.uuid4()}-attachment.txt"
25# Save the actual file in the results directory
26with open(f"allure-results/{file_name}", "w") as f:
27 f.write(content)
28# Register the attachment in the report
29lifecycle.add_attachment(source=file_name, name="Example Attachment", type="text/plain")
30
31# 5. Stop the Step
32lifecycle.stop_step(step_uuid, status=Status.PASSED)
33
34# 6. Stop and Write the Test Case
35lifecycle.stop_test_case(test_uuid, status=Status.PASSED)
36lifecycle.write_test_case(test_uuid)