@Patch decorator is not compatible with pytest fixture

When using pytest fixture with mock.patch, test parameter order is crucial.

If you place a fixture parameter before a mocked one:

from unittest import mock

@mock.patch('my.module.my.class')
def test_my_code(my_fixture, mocked_class):

then the mock object will be in my_fixture and mocked_class will be search as a fixture:

fixture 'mocked_class' not found

But, if you reverse the order, placing the fixture parameter at the end:

from unittest import mock

@mock.patch('my.module.my.class')
def test_my_code(mocked_class, my_fixture):

then all will be fine.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)