Message410667
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. |
|
Date |
User |
Action |
Args |
2022-01-16 03:21:02 | roysmith | set | recipients:
+ roysmith, cjw296, michael.foord, lisroach, corona10, mariocj89, xtreak, dmitriy.mironiyk, kamilturek, mardoxx |
2022-01-16 03:21:02 | roysmith | set | messageid: <1642303262.86.0.891937238502.issue43371@roundup.psfhosted.org> |
2022-01-16 03:21:02 | roysmith | link | issue43371 messages |
2022-01-16 03:21:02 | roysmith | create | |
|