LoginSignup
0
0

More than 3 years have passed since last update.

pytestのテストクラスの全メソッドにmonkeypatchを適用する

Posted at

失敗

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

以下のエラーを返す。
TypeError: setup_class() missing 1 required positional argument: 'monkeypatch'

成功

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()

テストクラス内にfixtureを定義し、autouse=Trueとすることでmonkeypatchがクラス内の全メソッドに適用される。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0