Title: Use TestCase assertion methods in unittest.mock.assert* to make them easier to read
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.10, Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: michael.foord Nosy List: Julian, eric.araujo, ezio.melotti, michael.foord, xtreak
Priority: normal Keywords:

Created on 2012-09-04 13:06 by Julian, last changed 2020-11-18 12:25 by xtreak.

Messages (4)
msg169826 - (view) Author: Julian Berman (Julian) * Date: 2012-09-04 13:06
Mock's assertion failures can be extremely hard to read for a few reasons -- mostly the noisy default repr that mock objects have, but also because they don't give you the hints that `unittest.TestCase`'s `assert*` methods give you (things like diffing two lists for example).

unittest.mock.Mock's `assert*` methods could hook into the TestCase's assertion methods if Mock either gained a MockInTestCase subclass or started taking an arg to `__init__` that was an instance of `TestCase`, so that `assert*` could then use the assertion methods on the instance.

#11664 could (should) then obviously use this argument by default when patching.

Another added advantage would be that the raised exception could then be `TestCase.failureException`, whatever that might be, rather than `AssertionError`, though I doubt that's that big a deal since that's usually a subclass of `AssertionError` I bet.
msg170303 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2012-09-11 13:15
Yep, interesting idea. In Python 3 you can't import unittest.mock without importing unittest, so there could even be a "default testcase" to fall back on. For the external release I'd rather not have unittest as a dependency, but allowing a testcase to be provided at instantiation would be fine.
msg170304 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2012-09-11 13:15
The only issue is that the testcase would have to be propagated to child mocks too.
msg381330 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-11-18 12:25
See also
Date User Action Args
2020-11-18 12:25:52xtreaksetnosy: + xtreak
messages: + msg381330
2020-11-18 11:46:59iritkatrielsettype: enhancement
versions: + Python 3.9, Python 3.10, - Python 3.4
2012-09-11 15:00:36eric.araujosetnosy: + eric.araujo

versions: - Python 3.3
2012-09-11 13:15:37michael.foordsetmessages: + msg170304
2012-09-11 13:15:13michael.foordsetassignee: michael.foord
messages: + msg170303
2012-09-04 13:16:47ezio.melottisetnosy: + ezio.melotti
2012-09-04 13:06:59Juliancreate