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 roysmith
Recipients cjw296, corona10, dmitriy.mironiyk, kamilturek, lisroach, mardoxx, mariocj89, michael.foord, roysmith, xtreak
Date 2022-01-16.03:21:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1642303262.86.0.891937238502.issue43371@roundup.psfhosted.org>
In-reply-to
Content
I agree that this is confusing and that what we need is an assertion for the top-level mock having specific calls in a specific order, and ignores any intervening extra calls to mocked functions.  In other words, a version of assert_has_calls() which looks at call_args_list instead of mock_calls.

I just finished up a session of head-banging with some tests that were failing (Python 3.7), and eventually ended up with the

self.assertEqual(my_mock.call_args_list, [call(...), call(...)])

idiom as noted in msg397172 (but without first banging a few new dents into the top of my desk).  This exact same experience is related in a recent stackoverflow thread (https://stackoverflow.com/questions/69360318/python-unittest-mock-assert-has-calls-returning-calls-to-other-mocks) so this seems to be a common source of confusion.

I am neutral on whether this is implemented as a new flag to assert_has_calls() or as a new assertion method.

As an aside, what I was trying to do was test if my code constructed its several instances of a class in the correct way.  At one point I hit upon the idea of:

MyMockedClass().__init__.assert_has_calls(....)

which expressed my desired logic exactly and simply, but couldn't get that to work.  It's unclear if I just never found the proper incantation, or if that's fundamentally unworkable.
History
Date User Action Args
2022-01-16 03:21:02roysmithsetrecipients: + roysmith, cjw296, michael.foord, lisroach, corona10, mariocj89, xtreak, dmitriy.mironiyk, kamilturek, mardoxx
2022-01-16 03:21:02roysmithsetmessageid: <1642303262.86.0.891937238502.issue43371@roundup.psfhosted.org>
2022-01-16 03:21:02roysmithlinkissue43371 messages
2022-01-16 03:21:02roysmithcreate