Skip to content

Commit e684571

Browse files
authored
pytest-bdd fixes (fixes #473 fixes #468 via #522)
1 parent 09aa3a4 commit e684571

File tree

14 files changed

+195
-163
lines changed

14 files changed

+195
-163
lines changed
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1 @@
1-
Feature: Scenario
2-
Scenario: Default labels
3-
Given feature file aaa with content:
4-
"""
5-
Feature: Scenario example
6-
Background:
7-
Given passed step1
8-
Scenario: Scenario example
9-
Given passed step
10-
When failed step
11-
Then passed step
12-
"""
13-
And dummy steps in conftest.py
14-
And test file with "Scenario example" scenario in example
15-
When run pytest-bdd with allure
16-
Then it has result for example scenario
1+
# ToDo ...
Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,51 @@
1-
Feature: Scenario
2-
Scenario: Default labels
3-
Given feature file aaa with content:
1+
Feature: Scenario outline
2+
Scenario: Scenario outline
3+
Given example.feature with content:
44
"""
5-
Feature: Scenario example
6-
Scenario Outline: Scenario example
7-
Given passed step
8-
When <status> step
9-
Then passed step
5+
Feature: Scenario outline
6+
Scenario Outline: Outline example
7+
Given <first> step
8+
When do nothing
9+
Then step with <second> param
1010
1111
Examples:
12-
| status |
13-
| passed |
14-
| failed |
12+
| first | second |
13+
| Alpha | 1 |
14+
| Bravo | 2 |
15+
"""
16+
And example_test.py with content:
17+
"""
18+
from pytest_bdd import scenario
19+
from pytest_bdd import given, then, when
20+
21+
@given("<first> step")
22+
def given_step(first):
23+
pass
24+
25+
@when("do nothing")
26+
def nope_step():
27+
pass
28+
29+
@then("step with <second> param")
30+
def then_step(second):
31+
pass
32+
33+
@scenario("example.feature", "Outline example")
34+
def test_scenario_outline_example():
35+
pass
1536
"""
16-
And dummy steps in conftest.py
17-
And test file with "Scenario example" scenario in example
1837
When run pytest-bdd with allure
19-
Then it has result for example scenario
38+
39+
Then allure report has result for "Outline example" scenario
40+
Then this scenario has parameter "first" with value "Alpha"
41+
Then this scenario has parameter "second" with value "1"
42+
Then this scenario contains "Given <Alpha> step" step
43+
Then this scenario contains "Then step with <1> param" step
44+
45+
Then allure report has result for "Outline example" scenario
46+
Then this scenario has parameter "first" with value "Bravo"
47+
Then this scenario has parameter "second" with value "2"
48+
Then this scenario contains "Given <Bravo> step" step
49+
Then this scenario contains "Then step with <2> param" step
50+
51+
Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
Feature: Scenario
2-
Scenario: Default labels
3-
Given feature file aaa with content:
2+
Scenario: Simple passed scenario
3+
Given example.feature with content:
44
"""
5-
Feature: Scenario example
6-
Scenario: Scenario example
5+
Feature: Scenario
6+
Scenario: Simple passed example
77
Given passed step
8-
When failed step
8+
When passed step
99
Then passed step
1010
"""
11-
And dummy steps in conftest.py
12-
And test file with "Scenario example" scenario in example
11+
And example_test.py with content:
12+
"""
13+
from pytest_bdd import scenario
14+
from pytest_bdd import given, then, when
15+
16+
@given("passed step")
17+
def given_passed_step():
18+
pass
19+
20+
@when("passed step")
21+
@then("passed step")
22+
def passed_step():
23+
pass
24+
25+
@scenario("example.feature", "Simple passed example")
26+
def test_scenario_example():
27+
pass
28+
"""
1329
When run pytest-bdd with allure
14-
Then it has result for example scenario
30+
Then allure report has result for "Simple passed example" scenario
31+
Then this scenario has passed status

allure-pytest-bdd/src/pytest_bdd_listener.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from allure_commons.model2 import StatusDetails
1616
from functools import partial
1717
from allure_commons.lifecycle import AllureLifecycle
18-
from .utils import get_full_name
18+
from .utils import get_full_name, get_name, get_params
1919

2020

2121
class PytestBDDListener(object):
@@ -36,14 +36,17 @@ def _scenario_finalizer(self, scenario):
3636
def pytest_bdd_before_scenario(self, request, feature, scenario):
3737
uuid = get_uuid(request.node.nodeid)
3838
full_name = get_full_name(feature, scenario)
39+
name = get_name(request.node, scenario)
3940
with self.lifecycle.schedule_test_case(uuid=uuid) as test_result:
4041
test_result.fullName = full_name
42+
test_result.name = name
4143
test_result.start = now()
4244
test_result.labels.append(Label(name=LabelType.HOST, value=self.host))
4345
test_result.labels.append(Label(name=LabelType.THREAD, value=self.thread))
4446
test_result.labels.append(Label(name=LabelType.FRAMEWORK, value="pytest-bdd"))
4547
test_result.labels.append(Label(name=LabelType.LANGUAGE, value=platform_label()))
4648
test_result.labels.append(Label(name=LabelType.FEATURE, value=feature.name))
49+
test_result.parameters = get_params(request.node)
4750

4851
finalizer = partial(self._scenario_finalizer, scenario)
4952
request.node.addfinalizer(finalizer)
@@ -59,7 +62,7 @@ def pytest_bdd_before_step_call(self, request, feature, scenario, step, step_fun
5962
parent_uuid = get_uuid(request.node.nodeid)
6063
uuid = get_uuid(str(id(step)))
6164
with self.lifecycle.start_step(parent_uuid=parent_uuid, uuid=uuid) as step_result:
62-
step_result.name = get_step_name(step)
65+
step_result.name = get_step_name(request.node, step)
6366

6467
@pytest.hookimpl
6568
def pytest_bdd_after_step(self, request, feature, scenario, step, step_func, step_func_args):

allure-pytest-bdd/src/utils.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,24 @@
33
from allure_commons.utils import md5
44
from allure_commons.model2 import StatusDetails
55
from allure_commons.model2 import Status
6+
from allure_commons.model2 import Parameter
67
from allure_commons.utils import format_exception
78

89

9-
def get_step_name(step):
10-
return "{step_keyword} {step_name}".format(step_keyword=step.keyword, step_name=step.name)
10+
def get_step_name(node, step):
11+
name = "{step_keyword} {step_name}".format(step_keyword=step.keyword, step_name=step.name)
12+
if hasattr(node, 'callspec'):
13+
for key, value in node.callspec.params.items():
14+
name = name.replace("<{key}>".format(key=key), "<{{{key}}}>".format(key=key))
15+
name = name.format(**node.callspec.params)
16+
return name
17+
18+
19+
def get_name(node, scenario):
20+
if hasattr(node, 'callspec'):
21+
parts = node.nodeid.rsplit("[")
22+
return "{name} [{params}".format(name=scenario.name, params=parts[-1])
23+
return scenario.name
1124

1225

1326
def get_full_name(feature, scenario):
@@ -31,3 +44,9 @@ def get_pytest_report_status(pytest_report):
3144
for pytest_status, status in zip(pytest_statuses, statuses):
3245
if getattr(pytest_report, pytest_status):
3346
return status
47+
48+
49+
def get_params(node):
50+
if hasattr(node, 'callspec'):
51+
params = node.callspec.params
52+
return [Parameter(name=name, value=value) for name, value in params.items()]

allure-pytest-bdd/test/conftest.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import allure_commons
55
from allure_commons_test.report import AllureReport
66
from allure_commons.logger import AllureFileLogger
7-
from .pytest_bdd_steps import * # noqa F401 F403
8-
from .report_steps import * # noqa F401 F403
7+
from .steps import * # noqa F401 F403
8+
from pytest_bdd import given, when, parsers
99

1010

1111
@contextmanager
@@ -32,12 +32,32 @@ def __init__(self, testdir, request):
3232
def run_with_allure(self):
3333
logger = AllureFileLogger(self.testdir.tmpdir.strpath)
3434
with fake_logger("allure_pytest_bdd.plugin.AllureFileLogger", logger):
35-
a = self.testdir.runpytest("-s", "-v", "--alluredir", self.testdir.tmpdir)
36-
print(a.stdout.lines)
37-
print(a.stderr.lines)
35+
self.testdir.runpytest("-s", "-v", "--alluredir", self.testdir.tmpdir)
36+
# print(a.stdout.lines)
37+
# print(a.stderr.lines)
3838
self.allure_report = AllureReport(self.testdir.tmpdir.strpath)
3939

4040

4141
@pytest.fixture
4242
def allured_testdir(testdir, request):
4343
return AlluredTestdir(testdir, request)
44+
45+
46+
@pytest.fixture
47+
def context():
48+
return dict()
49+
50+
51+
@pytest.fixture
52+
def allure_report(allured_testdir, context):
53+
return allured_testdir.allure_report
54+
55+
56+
@given(parsers.re("(?P<name>\\w+)(?P<extension>\\.\\w+) with content:(?:\n)(?P<content>[\\S|\\s]*)"))
57+
def feature_definition(name, extension, content, testdir):
58+
testdir.makefile(extension, **dict([(name, content)]))
59+
60+
61+
@when("run pytest-bdd with allure")
62+
def run(allured_testdir):
63+
allured_testdir.run_with_allure()

allure-pytest-bdd/test/dummy_steps.py

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from pytest_bdd import scenario
2+
3+
4+
@scenario("../features/outline.feature", "Scenario outline")
5+
def test_scenario_outline():
6+
pass

allure-pytest-bdd/test/pytest_bdd_steps.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

allure-pytest-bdd/test/pytest_bdd_test.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)