From cda84fb5669dec0acd8eb9963f76d7dac7a44392 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sat, 6 Oct 2012 21:03:55 +0200 Subject: [PATCH] - allow to use fixtures directly, i.e. without () - also allow scope to be determined by a dynamic function --- _pytest/__init__.py | 2 +- _pytest/python.py | 20 ++++++++++++++------ setup.py | 2 +- testing/test_python.py | 15 ++++++++++++++- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/_pytest/__init__.py b/_pytest/__init__.py index e8a1b700e..f1e87cf85 100644 --- a/_pytest/__init__.py +++ b/_pytest/__init__.py @@ -1,2 +1,2 @@ # -__version__ = '2.3.0.dev18' +__version__ = '2.3.0.dev19' diff --git a/_pytest/python.py b/_pytest/python.py index 09a417743..2b400d2cd 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -26,19 +26,27 @@ 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. """ - return FixtureFunctionMarker(scope, params, autoactive=autoactive) + 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() diff --git a/setup.py b/setup.py index 1489e3ad3..de0925a99 100644 --- a/setup.py +++ b/setup.py @@ -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'], diff --git a/testing/test_python.py b/testing/test_python.py index 7fc6af507..698cfcf62 100644 --- a/testing/test_python.py +++ b/testing/test_python.py @@ -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