Skip to content

Commit e76b066

Browse files
Merge pull request #188 from The-Compiler/tags_with_spaces
Allow spaces in tags
2 parents 30947a4 + 8e89d04 commit e76b066

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

CHANGES.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
Changelog
22
=========
33

4-
2.16.2
4+
2.17.0
55
------
66

77
- Fix FixtureDef signature for newer pytest versions (The-Compiler)
88
- Better error explanation for the steps defined outside of scenarios (olegpidsadnyi)
99
- Add a ``pytest_bdd_apply_tag`` hook to customize handling of tags (The-Compiler)
10+
- Allow spaces in tag names. This can be useful when using the
11+
``pytest_bdd_apply_tag`` hook with tags like ``@xfail: Some reason``.
1012

1113

1214
2.16.1

pytest_bdd/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
from pytest_bdd.steps import given, when, then
44
from pytest_bdd.scenario import scenario, scenarios
55

6-
__version__ = '2.16.2'
6+
__version__ = '2.17.0'
77

88
__all__ = [given.__name__, when.__name__, then.__name__, scenario.__name__, scenarios.__name__]

pytest_bdd/feature.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,10 @@ def get_tags(line):
134134
135135
:return: List of tags.
136136
"""
137+
if not line or '@' not in line.strip():
138+
return set()
137139
return (
138-
set((tag[1:] for tag in line.split() if tag.startswith("@") and len(tag) > 1))
139-
if line else set()
140+
set((tag.lstrip('@') for tag in line.strip().split(' @') if len(tag) > 1))
140141
)
141142

142143

tests/feature/test_tags.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Test tags."""
2-
from pytest_bdd import scenario
2+
import pytest
3+
4+
from pytest_bdd import scenario, feature
35

46

57
def test_tags(request):
@@ -139,3 +141,48 @@ def i_have_bar():
139141
"*= 2 skipped * =*",
140142
],
141143
)
144+
145+
146+
def test_tag_with_spaces(testdir):
147+
testdir.makeconftest("""
148+
import pytest
149+
150+
@pytest.hookimpl(tryfirst=True)
151+
def pytest_bdd_apply_tag(tag, function):
152+
assert tag == 'test with spaces'
153+
""")
154+
testdir.makefile('.feature', test="""
155+
Feature: Tag with spaces
156+
157+
@test with spaces
158+
Scenario: Tags
159+
Given I have a bar
160+
""")
161+
testdir.makepyfile("""
162+
from pytest_bdd import given, scenarios
163+
164+
@given('I have a bar')
165+
def i_have_bar():
166+
return 'bar'
167+
168+
scenarios('test.feature')
169+
""")
170+
result = testdir.runpytest_subprocess()
171+
result.stdout.fnmatch_lines(
172+
[
173+
"*= 1 passed * =*",
174+
],
175+
)
176+
177+
178+
@pytest.mark.parametrize('line, expected', [
179+
('@foo @bar', {'foo', 'bar'}),
180+
('@with spaces @bar', {'with spaces', 'bar'}),
181+
('@double @double', {'double'}),
182+
(' @indented', {'indented'}),
183+
(None, set()),
184+
('foobar', set()),
185+
('', set()),
186+
])
187+
def test_get_tags(line, expected):
188+
assert feature.get_tags(line) == expected

0 commit comments

Comments
 (0)