Mockライブラリを使用してアプリケーションをテストしていますが、一部の関数が呼び出されなかったと主張します。モックドキュメントはやのようなメソッドについて話しますがmock.assert_called_with
、モックが呼び出されmock.assert_called_once_with
なかったことmock.assert_not_called
を確認するためのようなものや何かが見つかりませんでした。
私は次のようなもので行くことができますが、それはクールでもPythonicでもないようです:
def test_something:
# some actions
with patch('something') as my_var:
try:
# args are not important. func should never be called in this test
my_var.assert_called_with(some, args)
except AssertionError:
pass # this error being raised means it's ok
# other stuff
これを達成する方法はありますか?
これはあなたのケースでうまくいくはずです。
assert not my_var.called, 'method should not have been called'
サンプル;
>>> mock=Mock()
>>> mock.a()
<Mock name='mock.a()' id='4349129872'>
>>> assert not mock.b.called, 'b was called and should not have been'
>>> assert not mock.a.called, 'a was called and should not have been'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: a was called and should not have been
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加