Assert that a method was called in a Python unit test

后端 未结 5 1242
不思量自难忘°
不思量自难忘° 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:50

    Yes if you are using Python 3.3+. You can use the built-in unittest.mock to assert method called. For Python 2.6+ use the rolling backport Mock, which is the same thing.

    Here is a quick example in your case:

    from unittest.mock import MagicMock
    aw = aps.Request("nv1")
    aw.Clear = MagicMock()
    aw2 = aps.Request("nv2", aw)
    assert aw.Clear.called
    

提交回复
热议问题