Skip to content

Commit 912bbe0

Browse files
authored
outside steps handling (via #267)
1 parent 7359599 commit 912bbe0

File tree

5 files changed

+95
-34
lines changed

5 files changed

+95
-34
lines changed

allure-robotframework/src/listener/allure_listener.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
import allure_commons
2+
from allure_commons.utils import now
23
from allure_commons.utils import uuid4
4+
from allure_commons.model2 import TestStepResult
5+
from allure_commons.model2 import Status, StatusDetails
6+
from allure_commons.model2 import Parameter
7+
from allure_commons.utils import format_exception, format_traceback
8+
9+
10+
def get_status(exception):
11+
if exception and isinstance(exception, AssertionError):
12+
return Status.FAILED
13+
elif exception:
14+
return Status.BROKEN
15+
return Status.PASSED
16+
17+
18+
def get_status_details(exc_type, exception, exc_traceback):
19+
if exception:
20+
return StatusDetails(message=format_exception(exc_type, exception),
21+
trace=format_traceback(exc_traceback))
322

423

524
class AllureListener(object):
@@ -13,3 +32,16 @@ def attach_data(self, body, name, attachment_type, extension):
1332
@allure_commons.hookimpl
1433
def attach_file(self, source, name, attachment_type, extension):
1534
self.logger.attach_file(uuid4(), source, name=name, attachment_type=attachment_type, extension=extension)
35+
36+
@allure_commons.hookimpl
37+
def start_step(self, uuid, title, params):
38+
parameters = [Parameter(name=name, value=value) for name, value in params.items()]
39+
step = TestStepResult(name=title, start=now(), parameters=parameters)
40+
self.logger.start_step(None, uuid, step)
41+
42+
@allure_commons.hookimpl
43+
def stop_step(self, uuid, exc_type, exc_val, exc_tb):
44+
self.logger.stop_step(uuid,
45+
stop=now(),
46+
status=get_status(exc_val),
47+
statusDetails=get_status_details(exc_type, exc_val, exc_tb))

allure-robotframework/test/library/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def should_not_has_tag(allure_report, test_case_matcher, tag):
5050

5151
def should_has_status(allure_report, item_matcher, status):
5252
status_matcher = partial(item_matcher, with_status, status)
53-
assert_that(allure_report, status_matcher)
53+
assert_that(allure_report, status_matcher())
5454

5555

5656
def should_has_step(allure_report, item_matcher, step):
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
*** Settings ***
2+
Library ./library/run.py
3+
Library ./library/test.py
4+
5+
*** Variables ***
6+
@{TC_OUTSIDE_STEP__LIBRARY}
7+
... from external import passed_outside_step, failed_outside_step
8+
...
9+
... def step_contains_passed_outside_step(): passed_outside_step()
10+
...
11+
... def step_contains_failed_outside_step(): failed_outside_step()
12+
13+
14+
@{TC_OUTSIDE_STEP__EXTERNAL_PY}
15+
... import allure
16+
...
17+
... @allure.step("Passed Outside Step")
18+
... def passed_outside_step(): pass
19+
...
20+
... @allure.step("Failed Outside Step")
21+
... def failed_outside_step(): assert False, "Hello there"
22+
23+
${TC_PASSED_OUTSIDE_STEP} | *Settings* | |\n
24+
... | Library | ./helper.py |\n
25+
... | | |\n
26+
... | *TestCase* | |\n
27+
... | Demo Test Case With Passed Outside Step | |\n
28+
... | | Step Contains Passed Outside Step |\n
29+
30+
${TC_FAILED_OUTSIDE_STEP} | *Settings* | |\n
31+
... | Library | ./helper.py |\n
32+
... | | |\n
33+
... | *TestCase* | |\n
34+
... | Demo Test Case With Failed Outside Step | |\n
35+
... | | Step Contains Failed Outside Step |\n
36+
37+
*** Test Cases ***
38+
Test Case With Passed Outside Step
39+
${tmp_dir}= Make Temp Dir
40+
${test_case_dir}= Make Dir ${tmp_dir} test
41+
Make File ${test_case_dir} helper.py ${TC_OUTSIDE_STEP__LIBRARY}
42+
Make File ${test_case_dir} external.py ${TC_OUTSIDE_STEP__EXTERNAL_PY}
43+
${test_case}= Make Test Case ${test_case_dir} step.robot ${TC_PASSED_OUTSIDE_STEP}
44+
${allure_report}= Robot Run With Allure ${tmp_dir} ${test_case}
45+
${tc_matcher}= Should Has Test Case ${allure_report} Demo Test Case With Passed Outside Step
46+
${step_matcher}= Should Has Step ${allure_report} ${tc_matcher} helper.Step Contains Passed Outside Step
47+
${outside_step_matcher}= Should Has Step ${allure_report} ${step_matcher} Passed Outside Step
48+
Should Has Status ${allure_report} ${outside_step_matcher} passed
49+
50+
Test Case With Failed Outside Step
51+
${tmp_dir}= Make Temp Dir
52+
${test_case_dir}= Make Dir ${tmp_dir} test
53+
Make File ${test_case_dir} helper.py ${TC_OUTSIDE_STEP__LIBRARY}
54+
Make File ${test_case_dir} external.py ${TC_OUTSIDE_STEP__EXTERNAL_PY}
55+
${test_case}= Make Test Case ${test_case_dir} step.robot ${TC_FAILED_OUTSIDE_STEP}
56+
${allure_report}= Robot Run With Allure ${tmp_dir} ${test_case}
57+
${tc_matcher}= Should Has Test Case ${allure_report} Demo Test Case With Failed Outside Step
58+
Should Has Status ${allure_report} ${tc_matcher} failed
59+
${step_matcher}= Should Has Step ${allure_report} ${tc_matcher} helper.Step Contains Failed Outside Step
60+
${outside_step_matcher}= Should Has Step ${allure_report} ${step_matcher} Failed Outside Step
61+
Should Has Status ${allure_report} ${outside_step_matcher} failed

allure-robotframework/test/step.robot

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,6 @@ ${TC_NESTED_STEPS_LAST_FAILED} | *TestCases*
3737
... | Parent Step | |\n
3838
... | | Fail |\n
3939

40-
${TC_OUTSIDE_STEP} | *Settings* | |\n
41-
... | Library | ./helper.py |\n
42-
... | | |\n
43-
... | *TestCase* | |\n
44-
... | Demo Test Case With Outside Step | |\n
45-
... | | Step Contains Outside Step |\n
46-
47-
@{TC_OUTSIDE_STEP__LIBRARY}
48-
... from external import outside_step
49-
...
50-
... def step_contains_outside_step(): outside_step()
51-
52-
@{TC_OUTSIDE_STEP__EXTERNAL_PY}
53-
... import allure
54-
...
55-
... @allure.step("Outside Step")
56-
... def outside_step(): pass
57-
5840

5941
*** Test Cases ***
6042
Test Case With Step
@@ -115,17 +97,3 @@ Test Case With Nested Steps Last Failed
11597
Should Has Status ${allure_report} ${parent_step_matcher} failed
11698
${step_matcher}= Should Has Step ${allure_report} ${parent_step_matcher} BuiltIn.Fail
11799
Should Has Status ${allure_report} ${step_matcher} failed
118-
119-
Test Case With Outside Step
120-
${tmp_dir}= Make Temp Dir
121-
${test_case_dir}= Make Dir ${tmp_dir} test
122-
Make File ${test_case_dir} helper.py ${TC_OUTSIDE_STEP__LIBRARY}
123-
Make File ${test_case_dir} external.py ${TC_OUTSIDE_STEP__EXTERNAL_PY}
124-
${test_case}= Make Test Case ${test_case_dir} step.robot ${TC_OUTSIDE_STEP}
125-
${allure_report}= Robot Run With Allure ${tmp_dir} ${test_case}
126-
${tc_matcher}= Should Has Test Case ${allure_report} Demo Test Case With Outside Step
127-
${step_matcher}= Should Has Step ${allure_report} ${tc_matcher} helper.Step Contains Outside Step
128-
# ToDo: Add hook handler to listener
129-
#${outside_step_matcher}= Should Has Step ${allure_report} ${step_matcher} Outside Step
130-
131-
# ToDo: Test Case With Outside Filed Step

allure-robotframework/tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ deps = robotframework
2020

2121
commands=
2222
python -m doctest -v ./src/listener/utils.py
23-
robot --loglevel DEBUG --listener allure_robotframework;{envtmpdir}/allure --outputdir {envtmpdir} ./test/
23+
robot --loglevel DEBUG --listener allure_robotframework;{envtmpdir}/allure --outputdir {envtmpdir} {posargs: ./test}
2424

2525

2626
[testenv:static_check]

0 commit comments

Comments
 (0)