Fixed step arguments conflict with the fixtures having the same name

This commit is contained in:
Oleg Pidsadnyi 2014-02-19 12:48:14 +01:00
parent 93b192e5f4
commit 92a9377cec
6 changed files with 137 additions and 21 deletions

View File

@ -1,6 +1,13 @@
Changelog
=========
0.6.10
-----
- Fixed step arguments conflict with the fixtures having the same name (olegpidsadnyi)
0.6.9
-----

View File

@ -56,28 +56,38 @@ def _find_step_function(request, name, encoding):
except python.FixtureLookupError:
for fixturename, fixturedefs in request._fixturemanager._arg2fixturedefs.items():
fixturedef = fixturedefs[0]
for fixturedef in fixturedefs:
pattern = getattr(fixturedef.func, 'pattern', None)
match = pattern.match(name) if pattern else None
pattern = getattr(fixturedef.func, 'pattern', None)
match = pattern.match(name) if pattern else None
if match:
for arg, value in match.groupdict().items():
fd = python.FixtureDef(
request._fixturemanager,
fixturedef.baseid,
arg,
lambda: value, fixturedef.scope, fixturedef.params,
fixturedef.unittest,
)
fd.cached_result = (value, 0)
# inject fixture definition
request._fixturemanager._arg2fixturedefs[arg] = [fd]
request._arg2fixturedefs[arg] = [fd]
# inject fixture value in request cache
getattr(request, '_fixturedefs', {})[arg] = fd
request._funcargs[arg] = value
return request.getfuncargvalue(pattern.pattern)
if match:
for arg, value in match.groupdict().items():
fd = python.FixtureDef(
request._fixturemanager,
fixturedef.baseid,
arg,
lambda: value, fixturedef.scope, fixturedef.params,
fixturedef.unittest,
)
fd.cached_result = (value, 0)
old_fd = getattr(request, '_fixturedefs', {}).get(arg)
old_value = request._funcargs.get(arg)
def fin():
request._fixturemanager._arg2fixturedefs[arg].remove(fd)
getattr(request, '_fixturedefs', {})[arg] = old_fd
request._funcargs[arg] = old_value
request.addfinalizer(fin)
# inject fixture definition
request._fixturemanager._arg2fixturedefs.setdefault(arg, []).insert(0, fd)
# inject fixture value in request cache
getattr(request, '_fixturedefs', {})[arg] = fd
request._funcargs[arg] = value
return request.getfuncargvalue(pattern.pattern)
raise

View File

@ -6,7 +6,7 @@ from setuptools import setup
from setuptools.command.test import test as TestCommand
version = '0.6.9'
version = '0.6.10'
class Tox(TestCommand):

0
tests/__init__.py Normal file
View File

View File

@ -0,0 +1,93 @@
import py
def test_arg_fixture_mix(testdir):
subdir = testdir.mkpydir("arg_fixture_mix")
subdir.join("test_a.py").write(py.code.Source("""
import re
import pytest
from pytest_bdd import scenario, given, then
@pytest.fixture
def foo():
return "fine"
test_args = scenario(
'arg_and_fixture_mix.feature',
'Use the step argument with the same name as fixture of another test',
)
@given(re.compile(r'foo is "(?P<foo>\w+)"'))
def foo1(foo):
pass
@then(re.compile(r'foo should be "(?P<foo_value>\w+)"'))
def foo_should_be(foo, foo_value):
assert foo == foo_value
test_bar = scenario(
'arg_and_fixture_mix.feature',
'Everything is fine',
)
@given(re.compile(r'it is all fine'))
def fine():
return "fine"
@then(re.compile(r'foo should be fine'))
def foo_should_be_fine(foo):
assert foo == "fine"
"""))
subdir.join("test_b.py").write(py.code.Source("""
import re
import pytest
from pytest_bdd import scenario, given, then
test_args = scenario(
'arg_and_fixture_mix.feature',
'Everything is fine',
)
@pytest.fixture
def foo():
return "fine"
@given(re.compile(r'it is all fine'))
def fine():
return "fine"
@then(re.compile(r'foo should be fine'))
def foo_should_be(foo):
assert foo == "fine"
def test_bar(foo):
assert foo == 'fine'
"""))
subdir.join("arg_and_fixture_mix.feature").write("""
Scenario: Use the step argument with the same name as fixture of another test
Given foo is "Hello"
Then foo should be "Hello"
Scenario: Everything is fine
Given it is all fine
Then foo should be fine
""")
result = testdir.runpytest("-k arg_fixture_mix")
assert result.ret == 0

View File

@ -24,5 +24,11 @@ deps =
hg+https://bitbucket.org/hpk42/py#egg=py
hg+https://bitbucket.org/hpk42/pytest#egg=pytest
[testenv:py27-pytest-2.4.2]
basepython=python2.7
deps =
{[testenv]deps}
pytest==2.4.2
[pytest]
pep8maxlinelength=120