How to properly use mock in python with unittest setUp

前端 未结 5 2173
北荒
北荒 2020-12-08 00:15

In my attempt to learn TDD, trying to learn unit testing and using mock with python. Slowly getting the hang of it, but unsure if I\'m doing this correctly. Forewarned: I\

5条回答
  •  被撕碎了的回忆
    2020-12-08 00:26

    You can use patch() as a class decorator, not just as a function decorator. You can then pass in the mocked function as before:

    @patch('mymodule.SomeClass')
    class MyTest(TestCase):
    
        def test_one(self, MockSomeClass):
            self.assertIs(mymodule.SomeClass, MockSomeClass)
    

    See: Applying the same patch to every test method (which also lists alternatives)

    It makes more sense to set up the patcher this way on setUp if you want the patching to be done for all the test methods.

提交回复
热议问题