Python:从unittest.mock解包call_args_列表
我想解压一个模拟方法的参数。我有一个被测试代码调用的模拟订户,我想验证notify()方法的调用 我使用以下代码片段来解压参数并验证两个调用:Python:从unittest.mock解包call_args_列表,python,python-3.x,mocking,python-unittest,python-unittest.mock,Python,Python 3.x,Mocking,Python Unittest,Python Unittest.mock,我想解压一个模拟方法的参数。我有一个被测试代码调用的模拟订户,我想验证notify()方法的调用 我使用以下代码片段来解压参数并验证两个调用: calls= self.subscriber.notify.call_args_list event1 = calls[0][0][0] event2 = calls[1][0][0] assert_that(event1, instance_of(CreatedEvent)) assert_that(event1.file.name, equal_t
calls= self.subscriber.notify.call_args_list
event1 = calls[0][0][0]
event2 = calls[1][0][0]
assert_that(event1, instance_of(CreatedEvent))
assert_that(event1.file.name, equal_to("foo.txt"))
但解压事件的两行代码非常笨拙,而且远离可读代码。
有人知道一个更好的方法来解开这些论点吗
非常感谢 如果您试图为每个调用中的事件断言相同的内容,那么使用简单的for循环可能会有所帮助:
for call in calls:
event = call[0][0]
assert_that(event, instance_of(CreatedEvent))
assert_that(event.file.name, equal_to("foo.txt"))
您可以使用
assert\u has\u调用
calls.assert_has_calls(call(ANY, ...), call(...))
您将如何处理
event1
和event2
?我不明白…更新了问题::-)
calls.assert_has_calls(call(ANY, ...), call(...))