I'm trying to conditionally mock something in a function-scoped fixture differently for different tests, so I don't have to create two entirely separate fixtures with nearly-identical code.
MY_GLOB_VAR = False
def mocked_build_data():
with conditional(MY_GLOB_VAR, mock.patch(stuff)):
build_data()
class Tests:
@pytest.fixture(scope='function')
def my_fixture(self, conftest_fixtures):
inject_test_specific_data_into_db(conftest_fixtures)
mocked_build_data()
yield
cleanup_mysql()
def test_1(self, my_fixture):
assert True
@mock.patch(f'{__name__}.MY_GLOB_VAR', True)
def test_2(self, my_fixture):
assert True
I need the mock on test_2 to be applied before my_fixture is executed, but it's only being applied once I'm in the function body.
Can I either force this the way I'm doing it, or conditionally mock the fixture some other way?