Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function scope fixture with failing finalizer called only once #498

Closed
pytestbot opened this issue Apr 3, 2014 · 5 comments
Closed

Function scope fixture with failing finalizer called only once #498

pytestbot opened this issue Apr 3, 2014 · 5 comments
Labels
type: bug problem that needs to be addressed

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Alexander Steinert (BitBucket: stony8, GitHub: stony8)


import pytest


class Helper:
    pass


@pytest.fixture(scope="function")
def failing_fixture(request):
    helper = Helper()
    def fin():
        raise RuntimeError("On purpose!")
    request.addfinalizer(fin)
    return helper


def test_0(failing_fixture):
    print("test_0", failing_fixture)


def test_1(failing_fixture):
    # In pytest 2.3.5 and 2.5.2, this test gets the same helper instance like
    # test_0 and the fixture's finalizer is not called.
    print("test_1", failing_fixture)
    assert 0
============================= test session starts ==============================
platform linux2 -- Python 3.2.3 -- py-1.4.20 -- pytest-2.5.2
collected 2 items

test_pytest.py .EF

==================================== ERRORS ====================================
_________________________ ERROR at teardown of test_0 __________________________

    def fin():
>       raise RuntimeError("On purpose!")
E       RuntimeError: On purpose!

test_pytest.py:12: RuntimeError
------------------------------- Captured stdout --------------------------------
test_0 <test_pytest.Helper object at 0x1be0710>
=================================== FAILURES ===================================
____________________________________ test_1 ____________________________________

failing_fixture = <test_pytest.Helper object at 0x1be0710>

    def test_1(failing_fixture):
        # In pytest 2.3.5 and 2.5.2, this test gets the same helper instance like
        # test_0 and the fixture's finalizer is not called.
        print("test_1", failing_fixture)
>       assert 0
E       assert 0

test_pytest.py:25: AssertionError
------------------------------- Captured stdout --------------------------------
test_1 <test_pytest.Helper object at 0x1be0710>
================= 1 failed, 1 passed, 1 error in 0.03 seconds ==================

@pytestbot
Copy link
Contributor Author

Original comment by Anton P. (BitBucket: anton7811, GitHub: anton7811):


The issues is reproduced on pytest 2.5.2

Additional info:

  1. In case we add more test cases, then the fixture will be created for the 3d test function but again skipped for the 4th one.
  2. The second test function receive a fixture that was created for the 1st test function. So it's state can be inconsistent. (Please check Peter Feiner's comment for te Fixture finalizer failure causes other finalizers to not be run #287 issue - https://bitbucket.org/hpk42/pytest/issue/287/fixture-finalizer-failure-causes-other)

I expect that function level fixture will be recreated for the next test function, or at least all other test functions will fail without launching. I don't see any reason why they have to be launched in case fixture cannot be set up.
In my case the first vaiant (force fixture recreation) is more preferable. At least it could be changed by some finalizer or fixture attribut.

@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


I have a fix in https://bitbucket.org/hpk42/pytest/pull-request/151/fix-issue498-if-a-fixture-finalizer-fails which will properly invalidate fixtures when their finalizers fail.

@pytestbot
Copy link
Contributor Author

Original comment by Ronny Pfannschmidt (BitBucket: RonnyPfannschmidt, GitHub: RonnyPfannschmidt):


Merged in hpk42/pytest-hpk/issue498 (pull request #151)

fix issue498: if a fixture finalizer fails, make sure that the fixture

@pytestbot
Copy link
Contributor Author

Original comment by Ronny Pfannschmidt (BitBucket: RonnyPfannschmidt, GitHub: RonnyPfannschmidt):


fix issue498: if a fixture finalizer fails, make sure that the fixture
is still invalidated.

@pytestbot
Copy link
Contributor Author

Original comment by Alexander Steinert (BitBucket: stony8, GitHub: stony8):


Fix verified for 9eeb7bb8cf81.

@pytestbot pytestbot added the type: bug problem that needs to be addressed label Jun 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

1 participant