This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mardoxx
Recipients cjw296, corona10, dmitriy.mironiyk, kamilturek, lisroach, mardoxx, mariocj89, michael.foord, xtreak
Date 2021-07-08.23:29:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1625786983.66.0.840527196182.issue43371@roundup.psfhosted.org>
In-reply-to
Content
This is surprising behaviour, Python 3.8.9:

factory = unittest.mock.Mock()

### test example
foo_obj = factory.create("foo"),do_thing()

# !! MUST BE CALLED AFTER .create("foo") !!
bar_obj = factory.create("bar").do_thing()
###

# I set any_order to false because these should be called in order.
factory.create.assert_has_calls([
    unittest.mock.call("foo"),
    unittest.mock.call("bar")
], any_order = False)

# OOPS!! this fails
# mock_calls == [call('foo'), call().do_thing(), call('bar'), call().do_thing()]
# I can however check this with...
# assert factory.create.call_args_list == [
#     unittest.mock.call("foo"),
#     unittest.mock.call("bar")
# ]

Am I fundamentally misunderstanding something here?

This appears to be different behaviour from assert_has_awaits, which adds to the confusion. assert_has_calls checks mock_calls, I'd have thought it should check call_args_list. Another method assert_mock_calls should check mock_calls for consistency? Or have I missed something here?

Perhaps an additional flag of in_sequence would be nice, defaulted to False to keep the existing unusual behaviour.
When False, calls must be in exactly the order they are described.
When True, calls must be in sequence (with anything in-between each call).
any_order=True overrides this and allows them to be in any order anywhere in the mock_calls list.
History
Date User Action Args
2021-07-08 23:29:43mardoxxsetrecipients: + mardoxx, cjw296, michael.foord, lisroach, corona10, mariocj89, xtreak, dmitriy.mironiyk, kamilturek
2021-07-08 23:29:43mardoxxsetmessageid: <1625786983.66.0.840527196182.issue43371@roundup.psfhosted.org>
2021-07-08 23:29:43mardoxxlinkissue43371 messages
2021-07-08 23:29:43mardoxxcreate