[PYTHON] Apply monkey patch to all methods of pytest test class

Failure

class TestHoge():
    def setup_class(cls, monkeypatch):
        monkeypatch.setattr(target, 'TargetObj', MockObj)

Returns the following error: TypeError: setup_class() missing 1 required positional argument: 'monkeypatch'

success

class TestHoge():
    @pytest.fixture(autouse=True)
    def patch(self, monkeypatch):
        monkeypatch.setattr(target, 'TargetCls', MockCls)

    def test_some_func(self):
        ret = target.TargetCls().some_func()

By defining fixture in the test class and setting autouse = True, monkey patch will be applied to all methods in the class.

Recommended Posts

Apply monkey patch to all methods of pytest test class
Example of using class variables and class methods
10 methods to improve the accuracy of BERT