Skip to content

Commit 1819b98

Browse files
authored
behave fixtures (fixes allure-framework#116 via allure-framework#127)
1 parent ab0235b commit 1819b98

File tree

11 files changed

+426
-127
lines changed

11 files changed

+426
-127
lines changed

allure-behave/features/background.feature

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ Feature: Background
2121

2222
Then allure report has a scenario with name "Scenario with background contains <step type> step"
2323
And this scenario has "<status>" status
24-
And this scenario has background "Scenario background with <step type> step"
25-
And this background has "<status>" status
26-
And this background contains step "Given <step type> step in background"
24+
And this scenario contains step "Given <step type> step in background"
2725
And this step has "<status>" status
28-
And this background contains step "And another passed step in background"
26+
And this scenario contains step "And another passed step in background"
2927
And this step has "<other status>" status
3028
And this scenario contains step "Given passed step"
3129
And this step has "<other status>" status
@@ -34,7 +32,6 @@ Feature: Background
3432

3533
Then allure report has a scenario with name "Another scenario with background contains <step type> step"
3634
And this scenario has "<status>" status
37-
And this scenario has background "Scenario background with <step type> step"
3835
And this scenario contains step "Given passed step"
3936
And this step has "<other status>" status
4037

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
Feature: Hook
2+
3+
Scenario Outline: Hook
4+
Given feature definition
5+
"""
6+
Feature: Hook
7+
Scenario: Scenario with "<when> <where>" hook
8+
Given simple passed step
9+
10+
Scenario: Another scenario with "<when> <where>" hook
11+
Given simple passed step
12+
"""
13+
And hooks implementation
14+
"""
15+
import allure_commons
16+
17+
18+
@allure_commons.fixture
19+
def <when>_<where>(context, <where>):
20+
pass
21+
"""
22+
When I run behave with allure formatter
23+
Then allure report has a scenario with name "Scenario with "<when> <where>" hook"
24+
And this scenario has <when> fixture "<when>_<where>"
25+
26+
Then allure report has a scenario with name "Another scenario with "<when> <where>" hook"
27+
And this scenario has <when> fixture "<when>_<where>"
28+
29+
Examples: fixtures
30+
| when | where |
31+
| before | scenario |
32+
| after | scenario |
33+
34+
35+
Scenario Outline: Hook
36+
Given feature definition
37+
"""
38+
Feature: Hook
39+
40+
@tag_for_hook
41+
Scenario: Scenario with "<when> <where>" hook
42+
Given simple passed step
43+
44+
Scenario: Another scenario without "<when> <where>" hook
45+
Given simple passed step
46+
"""
47+
And hooks implementation
48+
"""
49+
import allure_commons
50+
51+
52+
@allure_commons.fixture
53+
def <when>_<where>(context, <where>):
54+
pass
55+
"""
56+
When I run behave with allure formatter
57+
Then allure report has a scenario with name "Scenario with "<when> <where>" hook"
58+
And this scenario has <when> fixture "<when>_<where>"
59+
60+
Then allure report has a scenario with name "Another scenario without "<when> <where>" hook"
61+
And this scenario has not <when> fixture "<when>_<where>"
62+
63+
Examples: fixtures
64+
| when | where |
65+
| before | tag |
66+
| after | tag |
67+
68+
69+
Scenario Outline: Hook
70+
Given feature definition
71+
"""
72+
@tag_for_hook
73+
Feature: Hook
74+
75+
Scenario: Scenario with "<when> <where>" hook
76+
Given simple passed step
77+
78+
Scenario: Another scenario with "<when> <where>" hook
79+
Given simple passed step
80+
"""
81+
And hooks implementation
82+
"""
83+
import allure_commons
84+
85+
86+
@allure_commons.fixture
87+
def <when>_<where>(context, <where>):
88+
pass
89+
"""
90+
When I run behave with allure formatter
91+
Then allure report has a scenario with name "Scenario with "<when> <where>" hook"
92+
And this scenario has <when> fixture "<when>_<where>"
93+
94+
Then allure report has a scenario with name "Another scenario with "<when> <where>" hook"
95+
And this scenario has <when> fixture "<when>_<where>"
96+
97+
Examples: fixtures
98+
| when | where |
99+
| before | tag |
100+
| after | tag |
101+
102+
103+
Scenario Outline: Hook
104+
Given feature definition
105+
"""
106+
Feature: Hook
107+
Scenario: Scenario with "<when> <where>" hook
108+
Given simple passed step
109+
110+
Scenario: Another scenario with "<when> <where>" hook
111+
Given simple passed step
112+
"""
113+
And hooks implementation
114+
"""
115+
import allure_commons
116+
117+
118+
@allure_commons.fixture
119+
def <when>_<where>(context, <where>):
120+
pass
121+
"""
122+
When I run behave with allure formatter
123+
Then allure report has a scenario with name "Scenario with "<when> <where>" hook"
124+
And this scenario has <when> fixture "<when>_<where>"
125+
126+
Then allure report has a scenario with name "Another scenario with "<when> <where>" hook"
127+
And this scenario has <when> fixture "<when>_<where>"
128+
129+
Examples: fixtures
130+
| when | where |
131+
| before | feature |
132+
| after | feature |
133+
134+
135+
Scenario Outline: Hook
136+
Given feature definition
137+
"""
138+
Feature: Hook
139+
Scenario: Scenario with "<when> <where>" hook
140+
Given simple passed step
141+
142+
Scenario: Another scenario with "<when> <where>" hook
143+
Given simple passed step
144+
"""
145+
And hooks implementation
146+
"""
147+
import allure_commons
148+
149+
150+
@allure_commons.fixture
151+
def <when>_<where>(context):
152+
pass
153+
"""
154+
When I run behave with allure formatter
155+
Then allure report has a scenario with name "Scenario with "<when> <where>" hook"
156+
And this scenario has <when> fixture "<when>_<where>"
157+
158+
Then allure report has a scenario with name "Another scenario with "<when> <where>" hook"
159+
And this scenario has <when> fixture "<when>_<where>"
160+
161+
Examples: fixtures
162+
| when | where |
163+
| before | all |
164+
| after | all |

allure-behave/features/steps/behave_steps.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from behave.formatter.base import StreamOpener
99
import threading
1010

11+
A = None
12+
1113

1214
@given(u'feature definition')
1315
@given(u'feature definition {lang}')
@@ -16,10 +18,15 @@ def feature_definition(context, **kwargs):
1618
context.feature_definition = parser.parse(context.text)
1719

1820

21+
@given(u'hooks implementation')
22+
def hooks_implementations(context):
23+
context.globals = {}
24+
exec(context.text, context.globals)
25+
26+
1927
@when(u'I run behave with allure formatter')
2028
@when(u'I run behave with allure formatter with options "{args}"')
2129
def run_behave_with_allure(context, **kwargs):
22-
2330
def run(context, **kwargs):
2431
cmd_args = '-v -f allure_behave.formatter:AllureFormatter -f pretty'
2532
cmd = '{options} {cmd}'.format(cmd=cmd_args, options=kwargs.get('args', ''))
@@ -30,8 +37,10 @@ def run(context, **kwargs):
3037

3138
model_runner = ModelRunner(config, [context.feature_definition])
3239
model_runner.formatters = make_formatters(config, [stream_opener])
40+
model_runner.hooks = getattr(context, 'globals', dict())
3341
model_runner.run()
3442

43+
model_runner.formatters[0].listener.__del__()
3544
context.allure_report = AllureReport(result_tmp_dir)
3645

3746
behave_tread = threading.Thread(target=run, args=(context,), kwargs=kwargs)
Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from functools import partial
22
from hamcrest import assert_that
3+
from hamcrest import not_
34
from allure_commons_test.report import has_test_case
45
from allure_commons_test.result import with_status
56
from allure_commons_test.result import has_step
67
from allure_commons_test.result import has_attachment
78
from allure_commons_test.result import has_parameter
89
from allure_commons_test.container import has_container
9-
from allure_commons_test.container import has_before
10+
from allure_commons_test.container import has_before, has_after
1011
from allure_commons_test.label import has_severity
1112
from allure_commons_test.label import has_tag
1213

@@ -22,24 +23,49 @@ def match(matcher, *args):
2223

2324

2425
@then(u'allure report has a scenario with name "{scenario}"')
25-
def step_impl(context, scenario):
26+
def step_scenario(context, scenario):
2627
matcher = partial(match, has_test_case, scenario)
2728
context.scenario = matcher
2829
assert_that(context.allure_report, matcher())
2930

3031

31-
@then(u'scenario has background "{background}"')
32-
@then(u'this scenario has background "{background}"')
33-
def step_impl(context, background):
32+
@then(u'scenario has before fixture "{fixture}"')
33+
@then(u'this scenario has before fixture "{fixture}"')
34+
def step_before_fixture(context, fixture):
3435
context_matcher = context.scenario
35-
matcher = partial(context_matcher, has_container, context.allure_report, has_before, background)
36-
context.background = matcher
36+
matcher = partial(context_matcher, has_container, context.allure_report, has_before, fixture)
37+
context.before = matcher
38+
assert_that(context.allure_report, matcher())
39+
40+
41+
@then(u'scenario has after fixture "{fixture}"')
42+
@then(u'this scenario has after fixture "{fixture}"')
43+
def step_after_fixture(context, fixture):
44+
context_matcher = context.scenario
45+
matcher = partial(context_matcher, has_container, context.allure_report, has_after, fixture)
46+
context.after = matcher
47+
assert_that(context.allure_report, matcher())
48+
49+
50+
@then(u'scenario has not before fixture "{fixture}"')
51+
@then(u'this scenario has not before fixture "{fixture}"')
52+
def step_no_before_fixture(context, fixture):
53+
context_matcher = context.scenario
54+
matcher = partial(context_matcher, not_, has_container, context.allure_report, has_before, fixture)
55+
assert_that(context.allure_report, matcher())
56+
57+
58+
@then(u'scenario has not after fixture "{fixture}"')
59+
@then(u'this scenario has not after fixture "{fixture}"')
60+
def step_impl(context, fixture):
61+
context_matcher = context.scenario
62+
matcher = partial(context_matcher, not_, has_container, context.allure_report, has_after, fixture)
3763
assert_that(context.allure_report, matcher())
3864

3965

4066
@then(u'{item} contains step "{step}"')
4167
@then(u'this {item} contains step "{step}"')
42-
def step_impl(context, item, step):
68+
def step_step(context, item, step):
4369
context_matcher = getattr(context, item)
4470
matcher = partial(context_matcher, has_step, step)
4571
context.step = matcher
@@ -48,39 +74,39 @@ def step_impl(context, item, step):
4874

4975
@then(u'{item} has "{status}" status')
5076
@then(u'this {item} has "{status}" status')
51-
def step_impl(context, item, status):
77+
def step_status(context, item, status):
5278
context_matcher = getattr(context, item)
5379
matcher = partial(context_matcher, with_status, status)
5480
assert_that(context.allure_report, matcher())
5581

5682

5783
@then(u'scenario has "{severity}" severity')
5884
@then(u'this scenario has "{severity}" severity')
59-
def step_impl(context, severity):
85+
def step_severity(context, severity):
6086
context_matcher = context.scenario
6187
matcher = partial(context_matcher, has_severity, severity)
6288
assert_that(context.allure_report, matcher())
6389

6490

6591
@then(u'scenario has "{tag}" tag')
6692
@then(u'this scenario has "{tag}" tag')
67-
def step_impl(context, tag):
93+
def step_tag(context, tag):
6894
context_matcher = context.scenario
6995
matcher = partial(context_matcher, has_tag, tag)
7096
assert_that(context.allure_report, matcher())
7197

7298

7399
@then(u'{item} has parameter "{name}" with value "{value}"')
74100
@then(u'this {item} has parameter "{name}" with value "{value}"')
75-
def step_impl(context, item, name, value):
101+
def step_parameter(context, item, name, value):
76102
context_matcher = getattr(context, item)
77103
matcher = partial(context_matcher, has_parameter, name, value)
78104
assert_that(context.allure_report, matcher())
79105

80106

81107
@then(u'{item} has attachment')
82108
@then(u'this {item} has attachment')
83-
def step_impl(context, item):
109+
def step_attachment(context, item):
84110
context_matcher = getattr(context, item)
85111
matcher = partial(context_matcher, has_attachment)
86112
assert_that(context.allure_report, matcher())

0 commit comments

Comments
 (0)