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.

classification
Title: longMessage attribute is ignored in unittest.TestCase.assertRegexpMatches etc
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6, Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: berker.peksag Nosy List: Dhara, berker.peksag, bronger, copper-head, ezio.melotti, michael.foord, python-dev, rbcollins, wiggin15
Priority: normal Keywords: easy, patch

Created on 2014-01-23 09:39 by Dhara, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue20362.diff berker.peksag, 2014-05-18 05:08 review
20362_py27.patch copper-head, 2015-04-13 20:30 2.7 patch review
20362_py35.patch copper-head, 2015-04-13 20:31 3.5 patch
20362_py35_v2.patch copper-head, 2015-08-03 16:13 3.5 patch v2
Messages (15)
msg208899 - (view) Author: Dhara (Dhara) Date: 2014-01-23 09:39
Various assert methods added in version 2.7 ignore the longMessage attribute:
http://docs.python.org/2.7/library/unittest.html#unittest.TestCase.longMessage

For example when matching regular expressions using unittest.TestCase.assertRegexpMatches, the following error is generated:

AssertionError: <custom short message>: '<regexp>' not found in '<text>'

Since longMessage is ignored, and the text to be matched can be pages long, the error messages are impossibly long.

[See also: http://stackoverflow.com/questions/21157905/suppress-supplementary-error-message-from-unit-test]
msg218733 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2014-05-18 05:08
I've added missing self._formatMessage() functions to assertRegex and assertNotRegex asserts.
msg240737 - (view) Author: Ilia Kurenkov (copper-head) * Date: 2015-04-13 20:30
This fix has been retested with 2.7, I'm uploading the relevant patch files.
msg240740 - (view) Author: Ilia Kurenkov (copper-head) * Date: 2015-04-13 20:31
Modified to work with 3.5. Attaching patch file.
msg240745 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2015-04-13 20:56
Thanks for the patches, Ilia. The one for 2.7 looks good.

Reviewing 20362_py35.patch:

* Looks almost identical to issue20362.diff except style changes and the two comments below, right?

* -            self.assertIn("'kot'", e.args[0])

  Why did you remove this line?

* +    assertNotRegexpMatches = _deprecate(assertNotRegex)

  This looks like an unrelated change, but I think it can be committed. Thanks for the catch!

* The tests should be updated to use assertRegex and assertNotRegex
msg240836 - (view) Author: Ilia Kurenkov (copper-head) * Date: 2015-04-14 03:17
Thanks for the quick response, Berker! Let me address your comments in the order you made them.

#1: removing checking for "kot" in the exception args.
This line was causing the tests to fail, which is how I noticed it. The reason it fails (I think) is caused by the changes made in the patch. Namely, the fact that we're strictly sticking to longMessage=False means the custom message passed by the user (in this particular case that's "Message") is be the *only* thing sent to AssertionError. Thus checking for the presence of "kot" in this error's args doesn't seem to make much sense and will simply keep breaking our tests.
Now it goes without saying that I may have missed something, please let me know if that's the case.

#2
For some reason I got the impression that the tests should be using the newer method names, but since using the old ones didn't break them on my machine, I've updated the patch file accordingly. I'll upload it pending your feedback regarding #1.

Glad to help!
Ilia.
msg241635 - (view) Author: Ilia Kurenkov (copper-head) * Date: 2015-04-20 12:24
Hi Berker!

I hope all's well on your end. Let me know if you have questions about the reasoning behind my changes.
msg242880 - (view) Author: Ilia Kurenkov (copper-head) * Date: 2015-05-11 02:01
Hi there!

I was wondering if anyone had a chance to take a look at this :)
msg247888 - (view) Author: Ilia Kurenkov (copper-head) * Date: 2015-08-02 19:03
I notice folks have been subscribing to this. Any chance we could review it and merge? :)
msg247920 - (view) Author: Arnon Yaari (wiggin15) * Date: 2015-08-03 08:30
* The removal of the 'kot' test is ok. That's what the fix is about. With longMessage set to False, we should only see the custom message, 'Message', and not the message about the regex that contains 'kot'.
* Please update the patch for 3.5 to use assertRegex and assertNotRegex in the assertMessages call. These are the new names. With the current patch, the tests throw deprecation warnings.

Other than that the patch looks good (I am not a committer, though, so I can't help further).
msg247922 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2015-08-03 09:35
Sorry for my late response, Ilia. Your reply to my first question looks correct to me. Please send your updated patch and I'll apply.
msg247932 - (view) Author: Ilia Kurenkov (copper-head) * Date: 2015-08-03 16:13
Thanks for the feedback, folks. Adding the updated patch.
msg248422 - (view) Author: Ilia Kurenkov (copper-head) * Date: 2015-08-11 18:24
Bump :)

Let's close this one, guys!
msg248862 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-08-19 23:14
New changeset 738de9a9a3ea by Robert Collins in branch '3.5':
Issue #20362: Honour TestCase.longMessage correctly in assertRegex.
https://hg.python.org/cpython/rev/738de9a9a3ea

New changeset 977e60f597de by Robert Collins in branch 'default':
Issue #20362: Honour TestCase.longMessage correctly in assertRegex.
https://hg.python.org/cpython/rev/977e60f597de
msg248865 - (view) Author: Robert Collins (rbcollins) * (Python committer) Date: 2015-08-19 23:24
Thanks for the patch, applied to 3.5 and 3.6.
History
Date User Action Args
2022-04-11 14:57:57adminsetgithub: 64561
2015-08-19 23:24:52rbcollinssetstatus: open -> closed
resolution: fixed
messages: + msg248865

stage: commit review -> resolved
2015-08-19 23:14:02python-devsetnosy: + python-dev
messages: + msg248862
2015-08-19 23:03:41rbcollinssetstage: patch review -> commit review
2015-08-11 18:24:49copper-headsetmessages: + msg248422
2015-08-03 16:13:45copper-headsetfiles: + 20362_py35_v2.patch

messages: + msg247932
2015-08-03 09:35:52berker.peksagsetassignee: berker.peksag
messages: + msg247922
versions: + Python 3.4, Python 3.6
2015-08-03 08:30:03wiggin15setmessages: + msg247920
versions: - Python 3.4
2015-08-02 19:03:22copper-headsetmessages: + msg247888
2015-06-17 09:31:15brongersetnosy: + bronger
2015-05-17 21:58:03wiggin15setnosy: + wiggin15
2015-05-11 21:43:38ned.deilysetnosy: + rbcollins
2015-05-11 02:01:22copper-headsetmessages: + msg242880
2015-04-20 12:24:53copper-headsetmessages: + msg241635
2015-04-14 03:17:11copper-headsetmessages: + msg240836
2015-04-13 20:56:01berker.peksagsetmessages: + msg240745
2015-04-13 20:31:49copper-headsetfiles: + 20362_py35.patch

messages: + msg240740
2015-04-13 20:30:44copper-headsetfiles: + 20362_py27.patch
nosy: + copper-head
messages: + msg240737

2014-05-18 05:08:23berker.peksagsetfiles: + issue20362.diff

versions: - Python 3.3
keywords: + patch
nosy: + berker.peksag

messages: + msg218733
stage: needs patch -> patch review
2014-02-10 08:13:48ezio.melottisetkeywords: + easy
stage: needs patch
versions: + Python 3.3, Python 3.4, Python 3.5
2014-01-23 13:22:49berker.peksagsetnosy: + ezio.melotti, michael.foord
2014-01-23 09:39:37Dharacreate