I would like to apply a mock/patch that will apply to all tests, how can I do this?
I had tried putting it in a fixture, and using the fixture everywhere, but, the reapplication of the mock/patch on each test was leading to inconsistent id(my_mock)
values.
You can make a fixture applied only once for all the test suite execution by scoping the fixture to 'session'
, and using it in all your tests:
import pytest
from unittest import mock
@pytest.fixture(scope='session', autouse=True)
def my_thing_mock():
with mock.patch.object(TheThingYouWantToMock, 'some_attribute') as _fixture:
yield _fixture
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With