- allow to use fixtures directly, i.e. without ()

- also allow scope to be determined by a dynamic function
This commit is contained in:
holger krekel 2012-10-06 21:03:55 +02:00
parent d3893dd5d1
commit cda84fb566
4 changed files with 30 additions and 9 deletions

View File

@ -1,2 +1,2 @@
#
__version__ = '2.3.0.dev18'
__version__ = '2.3.0.dev19'

View File

@ -26,18 +26,26 @@ def fixture(scope=None, params=None, autoactive=False):
""" return a decorator to mark a fixture factory function.
The name of the fixture function can be referenced in a test context
to cause activation ahead of running tests. Test modules or classes
can use the pytest.mark.needsfixtures(fixturename) marker to specify
needed fixtures. Test functions can use fixture names as input arguments
in which case the object returned from the fixture function will be
injected.
to cause its invocation ahead of running tests. Test modules or classes
can use the pytest.mark.usefixtures(fixturename) marker to specify
needed fixtures. Test functions can also use fixture names as input
arguments in which case the fixture instance returned from the fixture
function will be injected.
:arg scope: the scope for which this fixture is shared, one of
"function", "class", "module", "session". Defaults to "function".
:arg params: an optional list of parameters which will cause multiple
invocations of the fixture functions and their dependent
tests.
:arg autoactive: if True, the fixture func is activated for all tests that
can see it. If False (the default) then an explicit
reference is needed to activate the fixture.
"""
if hasattr(scope, "__call__") and params is None and autoactive == False:
# direct decoration
return FixtureFunctionMarker(None, params, autoactive)(scope)
else:
return FixtureFunctionMarker(scope, params, autoactive=autoactive)
defaultfuncargprefixmarker = fixture()

View File

@ -24,7 +24,7 @@ def main():
name='pytest',
description='py.test: simple powerful testing with Python',
long_description = long_description,
version='2.3.0.dev18',
version='2.3.0.dev19',
url='http://pytest.org',
license='MIT license',
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

View File

@ -1701,7 +1701,20 @@ def test_issue117_sessionscopeteardown(testdir):
])
class TestFixtureFactory:
class TestFixtureUsages:
def test_noargfixturedec(self, testdir):
testdir.makepyfile("""
import pytest
@pytest.fixture
def arg1():
return 1
def test_func(arg1):
assert arg1 == 1
""")
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)
def test_receives_funcargs(self, testdir):
testdir.makepyfile("""
import pytest