Skip to content

Commit 097fcf6

Browse files
authored
Add logs, stdout and stderr to the allure-pytest-bdd report (#801)
1 parent d759bc5 commit 097fcf6

File tree

3 files changed

+162
-1
lines changed

3 files changed

+162
-1
lines changed

allure-pytest-bdd/src/pytest_bdd_listener.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from allure_commons.model2 import Label
66
from allure_commons.model2 import Status
77

8-
from allure_commons.types import LabelType
8+
from allure_commons.types import LabelType, AttachmentType
99
from allure_commons.utils import platform_label
1010
from allure_commons.utils import host_tag, thread_tag
1111
from allure_commons.utils import md5
@@ -114,6 +114,12 @@ def pytest_runtest_makereport(self, item, call):
114114
if test_result.status == Status.PASSED and status != Status.PASSED:
115115
test_result.status = status
116116
test_result.statusDetails = status_details
117+
if report.caplog:
118+
self.attach_data(report.caplog, "log", AttachmentType.TEXT, None)
119+
if report.capstdout:
120+
self.attach_data(report.capstdout, "stdout", AttachmentType.TEXT, None)
121+
if report.capstderr:
122+
self.attach_data(report.capstderr, "stderr", AttachmentType.TEXT, None)
117123

118124
if report.when == 'teardown':
119125
self.lifecycle.write_test_case(uuid=uuid)

tests/allure_pytest_bdd/acceptance/capture/__init__.py

Whitespace-only changes.
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import pytest
2+
from hamcrest import assert_that, empty
3+
from hamcrest import all_of, is_, is_not
4+
from hamcrest import has_property, has_value
5+
from hamcrest import contains_string
6+
from tests.allure_pytest.pytest_runner import AllurePytestRunner
7+
8+
9+
@pytest.mark.parametrize("capture", ["sys", "fd", "no"])
10+
def test_capture_stdout_in_bdd(allure_pytest_bdd_runner: AllurePytestRunner, capture):
11+
feature_content = (
12+
"""
13+
Feature: Basic allure-pytest-bdd usage
14+
Scenario: Simple passed example
15+
Given the preconditions are satisfied
16+
When the action is invoked
17+
Then the postconditions are held
18+
"""
19+
)
20+
steps_content = (
21+
"""
22+
from pytest_bdd import scenario, given, when, then
23+
@scenario("scenario.feature", "Simple passed example")
24+
def test_scenario_passes():
25+
pass
26+
27+
@given("the preconditions are satisfied")
28+
def given_the_preconditions_are_satisfied():
29+
print("Print from given step")
30+
31+
@when("the action is invoked")
32+
def when_the_action_is_invoked():
33+
print("Print from when step")
34+
35+
@then("the postconditions are held")
36+
def then_the_postconditions_are_held():
37+
print("Print from then step")
38+
"""
39+
)
40+
41+
allure_results = allure_pytest_bdd_runner.run_pytest(
42+
("scenario.feature", feature_content),
43+
steps_content, cli_args=(f"--capture={capture}",)
44+
)
45+
if_pytest_capture_ = is_not if capture == "no" else is_
46+
47+
assert_that(
48+
allure_results,
49+
has_property(
50+
"attachments",
51+
all_of(
52+
if_pytest_capture_(has_value(contains_string("Print from given step"))),
53+
if_pytest_capture_(has_value(contains_string("Print from when step"))),
54+
if_pytest_capture_(has_value(contains_string("Print from then step")))
55+
)
56+
)
57+
)
58+
59+
60+
@pytest.mark.parametrize("capture", ["sys", "fd"])
61+
def test_capture_empty_stdout(allure_pytest_bdd_runner: AllurePytestRunner, capture):
62+
feature_content = (
63+
"""
64+
Feature: Basic allure-pytest-bdd usage
65+
Scenario: Simple passed example
66+
Given the preconditions are satisfied
67+
When the action is invoked
68+
Then the postconditions are held
69+
"""
70+
)
71+
steps_content = (
72+
"""
73+
from pytest_bdd import scenario, given, when, then
74+
@scenario("scenario.feature", "Simple passed example")
75+
def test_scenario_passes():
76+
pass
77+
78+
@given("the preconditions are satisfied")
79+
def given_the_preconditions_are_satisfied():
80+
pass
81+
82+
@when("the action is invoked")
83+
def when_the_action_is_invoked():
84+
pass
85+
86+
@then("the postconditions are held")
87+
def then_the_postconditions_are_held():
88+
pass
89+
"""
90+
)
91+
92+
allure_results = allure_pytest_bdd_runner.run_pytest(
93+
("scenario.feature", feature_content),
94+
steps_content, cli_args=(f"--capture={capture}",)
95+
)
96+
97+
assert_that(
98+
allure_results,
99+
has_property("attachments", empty())
100+
)
101+
102+
103+
@pytest.mark.parametrize("logging", [True, False])
104+
def test_capture_log(allure_pytest_bdd_runner: AllurePytestRunner, logging):
105+
feature_content = (
106+
"""
107+
Feature: Basic allure-pytest-bdd usage
108+
Scenario: Simple passed example
109+
Given the preconditions are satisfied
110+
When the action is invoked
111+
Then the postconditions are held
112+
"""
113+
)
114+
steps_content = (
115+
"""
116+
import logging
117+
from pytest_bdd import scenario, given, when, then
118+
logger = logging.getLogger(__name__)
119+
@scenario("scenario.feature", "Simple passed example")
120+
def test_scenario_passes():
121+
pass
122+
123+
@given("the preconditions are satisfied")
124+
def given_the_preconditions_are_satisfied():
125+
logging.info("Logging from given step")
126+
127+
@when("the action is invoked")
128+
def when_the_action_is_invoked():
129+
logging.info("Logging from when step")
130+
131+
@then("the postconditions are held")
132+
def then_the_postconditions_are_held():
133+
logging.info("Logging from then step")
134+
"""
135+
)
136+
137+
params = [] if logging else ["-p", "no:logging"]
138+
allure_results = allure_pytest_bdd_runner.run_pytest(
139+
("scenario.feature", feature_content),
140+
steps_content, cli_args=("--log-level=INFO", *params)
141+
)
142+
143+
if_logging_ = is_ if logging else is_not
144+
145+
assert_that(
146+
allure_results,
147+
has_property(
148+
"attachments",
149+
all_of(
150+
if_logging_(has_value(contains_string("Logging from given step"))),
151+
if_logging_(has_value(contains_string("Logging from when step"))),
152+
if_logging_(has_value(contains_string("Logging from then step"))),
153+
)
154+
)
155+
)

0 commit comments

Comments
 (0)