question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add pytest.raises(..., contains='substring')

See original GitHub issue

pytest.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:closed
  • Created a year ago
  • Comments:29 (29 by maintainers)

github_iconTop GitHub Comments

3reactions
timhoffmcommented, Jun 28, 2022

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 by match. So the added complexity of fnmatch over contains is not needed.

In particular API-wise I find

with pytest.raises(ValueError, contains='...')

more readable than

with pytest.raises(ValueError, fnmatch='...')
1reaction
asottilecommented, Jun 27, 2022

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):

with pytest.raises(ValueError) as excinfo:
    do_thing()
assert 'Unsupported file type: *.py' in str(excinfo.value)
# or an exact check:
# msg, = excinfo.value.args
# assert msg == '...'
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found