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: New assert method that checks an error message for a list of strings
Type: enhancement Stage:
Components: Tests Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, ezio.melotti, iritkatriel, maciej.szulik, martin.panter, michael.foord, r.david.murray, rbcollins, serhiy.storchaka
Priority: normal Keywords:

Created on 2016-05-26 20:48 by maciej.szulik, last changed 2022-04-11 14:58 by admin.

Messages (9)
msg266454 - (view) Author: Maciej Szulik (maciej.szulik) * (Python triager) Date: 2016-05-26 20:48
To quote David from http://bugs.python.org/review/25591/diff/16398/Lib/test/test_imaplib.py:

"I've been thinking I'd like a new assert method that checks an error message for a list of strings in any order, but I haven't opened an issue for it :)"
msg266456 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-26 20:58
You can use assertRaisesRegex() for this.
msg266457 - (view) Author: Maciej Szulik (maciej.szulik) * (Python triager) Date: 2016-05-26 21:29
You could, but then you end up writing nasty regex-es to do that. The idea here is to create a native function that will verify a number of words in whatever order.
msg266461 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-05-26 23:28
Maybe a generic superset test would be good enough:

def assertSuperset(self, superset, subset):
    # Expand for friendlier failure handling
    self.assertTrue(all(e in superset for e in subset))

self.assertSuperset("Error message", ("mess", "or me"))

Or is that too obscure, treating a string as a set of substrings?
msg266472 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-27 04:13
This issue looks similar to issue24922, and I think it should be closed for same reasons.
msg266558 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-05-28 16:21
Serhiy, how do you spell "match if and only if all of the following substrings exist in the target string, in any position" in a regex?  If we aren't going to add the method, we should add an example of how to do this to the assertMsgRegex docs.
msg266564 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-28 17:31
The simple way is just write all possible variants ('ENABLE.*NOAUTH|NOAUTH.*ENABLE').

The general way is to use itertools.permutations():

    pattern = '|'.join(map('.*'.join, permutations(map(re.escape, strings))))

See also similar problem in issue19681. The first my patch used permutations(). But there was committed the patch with manually written variants.
msg266567 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-05-28 18:23
The complexity of those solutions argues in favor of adding a method, I'd say.

Personally I'd write multiple asserts rather than regex permutations.
msg415820 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-03-22 21:42
> Personally I'd write multiple asserts rather than regex permutations.

I would too, because then when one of them fails it's clear which of the strings is missing.
History
Date User Action Args
2022-04-11 14:58:31adminsetgithub: 71319
2022-03-22 21:42:01iritkatrielsetnosy: + iritkatriel
messages: + msg415820
2016-05-28 18:23:45r.david.murraysetmessages: + msg266567
2016-05-28 17:31:58serhiy.storchakasetmessages: + msg266564
2016-05-28 16:21:22r.david.murraysetmessages: + msg266558
2016-05-27 04:13:03serhiy.storchakasetnosy: + rbcollins, ezio.melotti, michael.foord, berker.peksag
messages: + msg266472
2016-05-26 23:28:00martin.pantersetnosy: + martin.panter
messages: + msg266461
2016-05-26 21:29:29maciej.szuliksetmessages: + msg266457
2016-05-26 20:58:36serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg266456
2016-05-26 20:48:13maciej.szulikcreate