fixture teardown does not seems to execute #9996
-
My tests directory structure is as follows:
contents of conftest.py on the top level is as follows: import pytest
from .helper import *
@pytest.fixture(scope='session')
def object_manager():
def _dispatcher(object_name, data, method ):
response = send_request(object_name, data, method) <--- creates object
print('Before yield')
yield response
print('After yield')
response = send_request(object_name, data, method) <-- deletes object
return _dispatcher I use this fixture in test_object1.py within object1 directory. The contents of test_object1.py is as follows: import pytest
def test_setup(object_manager):
response = next(object_manager(object_name=POD, data=values_for_new_object()))
assert response['success'] Then I run pytest with options to show setup for fixtures and capture set to no using the following command:
From this I can see that my fixture entered it's SETUP phase, where it created the object like it is supposed to, it even prints the 'Before yield' statement I put in there for debugging. Next the asserts in my test_setup function pass, I get the green dot too. However when the fixture enters TEARDOWN phase it does not seem to delete the object. There is nothing wrong with send_request() function , I checked. The statement "After yield" should be printed when the fixture enters TEARDOWN phase, I do not see it in the output. I feel like all the code below yield are not executed. Could you please tell me what went wrong here ? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @tr-gis,
Your fixture is returning a function, which you call on the test directly. From the fixture's point of view, there's no teardown phase, because you return an object from it (the The From your example, it doesn't seem like you need a fixture at all, and could just use a context-manager instead: # Implemented in some module importable by your app, say `app.testing`.
@contextmanager
def object_manager(object_name, data, method):
response = send_request(object_name, data, method)
print("Before yield")
try:
yield response
finally:
print("After yield")
response = send_request(object_name, data, method)
from app.testing import object_manager
def test_setup():
with object_manager(object_name=POD, data=values_for_new_object()) as response:
assert response['success'] Hope this helps. |
Beta Was this translation helpful? Give feedback.
Hi @tr-gis,
Your fixture is returning a function, which you call on the test directly. From the fixture's point of view, there's no teardown phase, because you return an object from it (the
object_manager
function), and that's it. The fixture can't know that your function yields a value, and that you expect that the function should continue and execute the code after the yield as teardown. The function object has been returned from the fixture, and pytest has no control over it anymore.The
yield
mechanism (setup untilyield
, teardown after) only works if theyield
statement is in the body of the fixture function; in your example, your fix…