From 522d59e844abe815790acf733a0aa13b597acfac Mon Sep 17 00:00:00 2001 From: Dmitry Malinovsky Date: Sat, 10 Dec 2016 16:45:40 +0600 Subject: [PATCH] Use session.config.hook instead of ihook. Fixes #2124 --- _pytest/fixtures.py | 8 +++---- testing/python/fixture.py | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/_pytest/fixtures.py b/_pytest/fixtures.py index 28bcd4d8d..c2a27b83d 100644 --- a/_pytest/fixtures.py +++ b/_pytest/fixtures.py @@ -754,8 +754,8 @@ class FixtureDef: func = self._finalizer.pop() func() finally: - ihook = self._fixturemanager.session.ihook - ihook.pytest_fixture_post_finalizer(fixturedef=self) + hook = self._fixturemanager.session.config.hook + hook.pytest_fixture_post_finalizer(fixturedef=self) # even if finalization fails, we invalidate # the cached fixture value if hasattr(self, "cached_result"): @@ -783,8 +783,8 @@ class FixtureDef: self.finish() assert not hasattr(self, "cached_result") - ihook = self._fixturemanager.session.ihook - return ihook.pytest_fixture_setup(fixturedef=self, request=request) + hook = self._fixturemanager.session.config.hook + return hook.pytest_fixture_setup(fixturedef=self, request=request) def __repr__(self): return ("" % diff --git a/testing/python/fixture.py b/testing/python/fixture.py index be99ed833..3e84be138 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -2984,4 +2984,49 @@ class TestParameterizedSubRequest: """.format(fixfile.strpath, testfile.basename)) +def test_pytest_fixture_setup_hook(testdir): + testdir.makeconftest(""" + import pytest + def pytest_fixture_setup(): + print('pytest_fixture_setup hook called') + """) + testdir.makepyfile(""" + import pytest + + @pytest.fixture() + def some(): + return 'some' + + def test_func(some): + assert some == 'some' + """) + result = testdir.runpytest("-s") + assert result.ret == 0 + result.stdout.fnmatch_lines([ + "*pytest_fixture_setup hook called*", + ]) + + +def test_pytest_fixture_post_finalizer_hook(testdir): + testdir.makeconftest(""" + import pytest + + def pytest_fixture_post_finalizer(): + print('pytest_fixture_post_finalizer hook called') + """) + testdir.makepyfile(""" + import pytest + + @pytest.fixture() + def some(): + return 'some' + + def test_func(some): + assert some == 'some' + """) + result = testdir.runpytest("-s") + assert result.ret == 0 + result.stdout.fnmatch_lines([ + "*pytest_fixture_post_finalizer hook called*", + ])