From 935761f09869f1144b715249a256602b0dd3f77d Mon Sep 17 00:00:00 2001 From: holger krekel Date: Wed, 8 Aug 2012 14:53:47 +0200 Subject: [PATCH] also improve missing funcarg error for setup functions --- _pytest/python.py | 42 +++++++++++++++++++---------------- testing/test_python.py | 50 +++++++++++++++++++++++++++++------------- 2 files changed, 58 insertions(+), 34 deletions(-) diff --git a/_pytest/python.py b/_pytest/python.py index 7e48023f5..3c1fb4508 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -949,9 +949,9 @@ class FuncargRequest: self.parentid) return facdeflist - #def raiseerror(self, msg): - # """ raise a FuncargLookupError with the given message. """ - # raise self.funcargmanager.FuncargLookupError(self.function, msg) + def raiseerror(self, msg): + """ raise a FuncargLookupError with the given message. """ + raise self.funcargmanager.FuncargLookupError(self.function, msg) @property def function(self): @@ -1375,22 +1375,26 @@ class FuncargManager: for setupcall in setuplist: if setupcall.active: continue - testcontext = TestContextSetup(request, setupcall) - kwargs = {} - for name in setupcall.funcargnames: - try: - kwargs[name] = request.getfuncargvalue(name) - except FuncargLookupError: - if name == "testcontext": - kwargs[name] = testcontext - else: - raise - scope = setupcall.scope or "function" - scol = setupcall.scopeitem = request._getscopeitem(scope) - self.session._setupstate.addfinalizer(setupcall.finish, scol) - for argname in setupcall.funcargnames: # XXX all deps? - self.addargfinalizer(setupcall.finish, argname) - setupcall.execute(kwargs) + request._factorystack.append(setupcall) + try: + testcontext = TestContextSetup(request, setupcall) + kwargs = {} + for name in setupcall.funcargnames: + try: + kwargs[name] = request.getfuncargvalue(name) + except FuncargLookupError: + if name == "testcontext": + kwargs[name] = testcontext + else: + raise + scope = setupcall.scope or "function" + scol = setupcall.scopeitem = request._getscopeitem(scope) + self.session._setupstate.addfinalizer(setupcall.finish, scol) + for argname in setupcall.funcargnames: # XXX all deps? + self.addargfinalizer(setupcall.finish, argname) + setupcall.execute(kwargs) + finally: + request._factorystack.remove(setupcall) def addargfinalizer(self, finalizer, argname): l = self._arg2finish.setdefault(argname, []) diff --git a/testing/test_python.py b/testing/test_python.py index 34cb871c1..90cd7d76b 100644 --- a/testing/test_python.py +++ b/testing/test_python.py @@ -2338,21 +2338,41 @@ class TestTestContextScopeAccess: reprec.assertoutcome(passed=1) -def test_illdefined_factory(testdir): - testdir.makepyfile(""" - import pytest - @pytest.factory() - def gen(request): - return 1 - def test_something(gen): - pass - """) - result = testdir.runpytest() - assert result.ret != 0 - result.stdout.fnmatch_lines([ - "*def gen(request):*", - "*no factory*request*", - ]) +class TestErrors: + def test_subfactory_missing_funcarg(self, testdir): + testdir.makepyfile(""" + import pytest + @pytest.factory() + def gen(request): + return 1 + def test_something(gen): + pass + """) + result = testdir.runpytest() + assert result.ret != 0 + result.stdout.fnmatch_lines([ + "*def gen(request):*", + "*no factory*request*", + "*1 error*", + ]) + + def test_setupfunc_missing_funcarg(self, testdir): + testdir.makepyfile(""" + import pytest + @pytest.setup() + def gen(request): + return 1 + def test_something(): + pass + """) + result = testdir.runpytest() + assert result.ret != 0 + result.stdout.fnmatch_lines([ + "*def gen(request):*", + "*no factory*request*", + "*1 error*", + ]) + class TestTestContextVarious: def test_newstyle_no_request(self, testdir):