Assert that a method was called in a Python unit test

后端 未结 5 1217
不思量自难忘°
不思量自难忘° 2020-12-04 15:14

Suppose I have the following code in a Python unit test:

aw = aps.Request(\"nv1\")
aw2 = aps.Request(\"nv2\", aw)

Is there an easy way to a

5条回答
  •  渐次进展
    2020-12-04 15:30

    You can mock out aw.Clear, either manually or using a testing framework like pymox. Manually, you'd do it using something like this:

    class MyTest(TestCase):
      def testClear():
        old_clear = aw.Clear
        clear_calls = 0
        aw.Clear = lambda: clear_calls += 1
        aps.Request('nv2', aw)
        assert clear_calls == 1
        aw.Clear = old_clear
    

    Using pymox, you'd do it like this:

    class MyTest(mox.MoxTestBase):
      def testClear():
        aw = self.m.CreateMock(aps.Request)
        aw.Clear()
        self.mox.ReplayAll()
        aps.Request('nv2', aw)
    

提交回复
热议问题