In my unit tests, I have two very similar fixtures, and I was hoping to break out some of the functionality into a helper function of some kind. Given my understanding of how yield
produces generators, I don't feel like this should cause any kind of problem. my_fixture_with_helper
, should just return the generator that fixture_helper
produces.
import pytest
def fixture_helper():
print("Initialized from the helper...")
yield 26
print("Tearing down after the helper...")
@pytest.fixture
def my_fixture_with_helper():
return fixture_helper()
@pytest.fixture
def my_fixture():
print("Initialized from the fixture...")
yield 26
print("Tearing down after the fixture...")
def test_nohelper(my_fixture):
pass
def test_helper(my_fixture_with_helper):
pass
However, if I run pytest --capture=no
, I get the following
test_foo.py Initialized from the fixture...
.Tearing down after the fixture...
.
I would expect "Initialized from the helper" and "Tearing down after the helper" to get printed, but it does not, and I cannot figure out why. Why does this not work?