Add pytest.raises(..., contains='substring')
See original GitHub issuepytest.raises can assert the message content as regexp via the match
parameter. However, most of the time, I really only need to check if a substring is present in the message. Regexp is quite a heavy machinery for that and I have to constantly think whether my expression is plain text or whether I have to escape literally or via re.escape
.
We currently have to do:
with pytest.raises(ValueError, match=r'Unsupported file type: \*\.py')
# or
with pytest.raises(ValueError, match=re.escape('Unsupported file type: *.py'))
I would additionally like to have:
with pytest.raises(ValueError, contains='Unsupported file type: *.py')
On the one hand, this is a bit redundant: You have the functionality with escaped regexes and the two keywords match and contains would be mutually exclusive. On the other hand, contains is much simpler to think about, and I’d claim sufficient for 90% of the use cases.
Question: Would you be open to a PR adding this?
Issue Analytics
- State:
- Created a year ago
- Comments:29 (29 by maintainers)
Top Results From Across the Web
How to write and report assertions in tests - Pytest
In order to write assertions about raised exceptions, you can use pytest.raises() as a context manager like this: import pytest def test_zero_division(): ...
Read more >Pytest/Hypothesis not matching string as expected
Why is my assert not matching? The strings look the same to me. I am assuming it's due to the float value, but...
Read more >How to Check if a Python String Contains a Substring
In this tutorial, you'll learn the best way to check whether a Python string contains a substring. You'll also learn about idiomatic ways...
Read more >tests/test_manager.py · version-0.1.5 · GREN_Map ... - Sign in
with pytest.raises(AttributeIdError): ... Adding institution with add_institution function of manager ... 'id__contains: substring',.
Read more >codesignal python - Caseificio de Nicola
There are two ways to add AWGN to the signal vector as follows. This list of monochrome and ... 12 mar 2020 21:05ㆍPython/CodeSignal...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
fnmatch would be okish but not my preferred solution. Here is why:
I have seen quite some testing code and the majority of use cases really only need
contains
(i.e. check that some words / a phrase is in the message).fnmatch
wildcards are simple and few enough that they don’t complicate that use case. OTOH the few cases that really need flexible matching can well be handled bymatch
. So the added complexity of fnmatch over contains is not needed.In particular API-wise I find
more readable than
I don’t think we should add this or startswith or endswith etc. that would naturally follow
it’s easy to either use match= or what I prefer (directly testing the stringification):