Skip to content

Commit

Permalink
Make feature and scenario tags to be fully compartible with pytest ma…
Browse files Browse the repository at this point in the history
…rkers. closes #112
  • Loading branch information
bubenkoff committed Jun 4, 2015
1 parent 697222b commit 8209470
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
@@ -1,6 +1,11 @@
Changelog
=========

2.10.0
------

- Make feature and scenario tags to be fully compartible with pytest markers (bubenkoff, kevinastone)

2.9.1
-----

Expand Down
9 changes: 6 additions & 3 deletions README.rst
Expand Up @@ -646,11 +646,14 @@ scenario test, so we can use standard test selection:

.. code-block:: bash
py.test -k "@backend and @login and @successful"
py.test -k "backend and login and successful"
The `@` helps to separate normal markers from the bdd ones.
The feature and scenario markers are not different from standard pytest markers, and the `@` symbol is stripped out
automatically to allow test selector expressions. If you want to have bdd-related tags to be distinguishable from the
other test markers, use prefix like `bdd`.
Note that if you use pytest `--strict` option, all bdd tags mentioned in the feature files should be also in the
`markers` setting of the `pytest.ini` config.
`markers` setting of the `pytest.ini` config. Also for tags please use names which are python-compartible variable
names, eg starts with a non-number, underscore alphanumberic, etc. That way you can safely use tags for tests filtering.


Test setup
Expand Down
2 changes: 1 addition & 1 deletion pytest_bdd/__init__.py
@@ -1,6 +1,6 @@
"""pytest-bdd public API."""

__version__ = '2.9.1'
__version__ = '2.10.0'

try:
from pytest_bdd.steps import given, when, then
Expand Down
2 changes: 1 addition & 1 deletion pytest_bdd/feature.py
Expand Up @@ -146,7 +146,7 @@ def get_tags(line):
:return: List of tags.
"""
return (
set((tag for tag in line.split() if tag.startswith("@") and len(tag) > 1))
set((tag[1:] for tag in line.split() if tag.startswith("@") and len(tag) > 1))
if line else set()
)

Expand Down
6 changes: 3 additions & 3 deletions tests/feature/tags.feature
@@ -1,10 +1,10 @@
@feature-tag-1 @feature-tag-2
@feature_tag_1 @feature_tag_2
Feature: Tags

@scenario-tag-1 @scenario-tag-2
@scenario_tag_1 @scenario_tag_2
Scenario: Tags
Given I have a bar

@scenario-tag-10 @scenario-tag-20
@scenario_tag_10 @scenario_tag_20
Scenario: Tags 2
Given I have a bar
6 changes: 3 additions & 3 deletions tests/feature/test_cucumber_json.py
Expand Up @@ -110,7 +110,7 @@ def test_failing():
],
"tags": [
{
'name': '@scenario-passing-tag',
'name': 'scenario-passing-tag',
'line': 4,
}
],
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_failing():
],
"tags": [
{
'name': '@scenario-failing-tag',
'name': 'scenario-failing-tag',
'line': 9,
}
],
Expand All @@ -164,7 +164,7 @@ def test_failing():
"name": "One passing scenario, one failing scenario",
"tags": [
{
'name': '@feature-tag',
'name': 'feature-tag',
'line': 1,
}
],
Expand Down
52 changes: 46 additions & 6 deletions tests/feature/test_tags.py
Expand Up @@ -11,13 +11,53 @@ def test_tags(request):
def test():
pass

assert test.__scenario__.tags == set(['@scenario-tag-1', '@scenario-tag-2'])
assert test.__scenario__.feature.tags == set(['@feature-tag-1', '@feature-tag-2'])
assert test.__scenario__.tags == set(['scenario_tag_1', 'scenario_tag_2'])
assert test.__scenario__.feature.tags == set(['feature_tag_1', 'feature_tag_2'])

assert getattr(test, '@scenario-tag-1')
assert getattr(test, '@scenario-tag-2')
assert getattr(test, 'scenario_tag_1')
assert getattr(test, 'scenario_tag_2')

assert getattr(test, '@feature-tag-1')
assert getattr(test, '@feature-tag-2')
assert getattr(test, 'feature_tag_1')
assert getattr(test, 'feature_tag_2')

test(request)


def test_tags_selector(testdir):
"""Test tests selection by tags."""
testdir.makefile('.feature', test="""
@feature_tag_1 @feature_tag_2
Feature: Tags
@scenario_tag_01 @scenario_tag_02
Scenario: Tags
Given I have a bar
@scenario_tag_10 @scenario_tag_20
Scenario: Tags 2
Given I have a bar
""")
testdir.makepyfile("""
import pytest
from pytest_bdd import given, scenarios
@given('I have a bar')
def i_have_bar():
return 'bar'
scenarios('test.feature')
""")
result = testdir.runpytest('-k', 'scenario_tag_10 and not scenario_tag_01', '-vv').parseoutcomes()
assert result['passed'] == 1
assert result['deselected'] == 1

result = testdir.runpytest('-k', 'scenario_tag_01 and not scenario_tag_10', '-vv').parseoutcomes()
assert result['passed'] == 1
assert result['deselected'] == 1

result = testdir.runpytest('-k', 'feature_tag_1', '-vv').parseoutcomes()
assert result['passed'] == 2

result = testdir.runpytest('-k', 'feature_tag_10', '-vv').parseoutcomes()
assert result['deselected'] == 2

0 comments on commit 8209470

Please sign in to comment.