How to properly use mock in python with unittest setUp

前端 未结 5 2159
北荒
北荒 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:39

    You can create a patched inner function and call it from setUp.

    If your original setUp function is:

    def setUp(self):
        some_work()
    

    Then you can patch it by changing it to:

    def setUp(self):
        @patch(...)
        def mocked_func():
            some_work()
    
        mocked_func()
    

提交回复
热议问题