Skip to content

Conversation

@vicky-dx
Copy link
Contributor

@vicky-dx vicky-dx commented Dec 2, 2025

Summary

This PR adds test coverage for using compiled regex patterns (re.Pattern objects) with the match parameter in pytest.raises(). While this functionality already works, it wasn't explicitly tested.

Changes

  • Added test_raises_match_compiled_regex() in testing/python/raises.py
  • Tests three scenarios:
    1. Compiled regex pattern that matches
    2. Compiled regex pattern that doesn't match (verifies error message)
    3. Compiled regex with flags (e.g., re.IGNORECASE)

Checklist

  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits.
  • Create a new changelog file in the changelog folder (will add after PR number is assigned)
  • Add yourself to AUTHORS in alphabetical order.

Notes

This is a test coverage improvement that ensures the existing functionality for compiled regex patterns remains stable in future releases.

vicky-dx added a commit to vicky-dx/pytest that referenced this pull request Dec 2, 2025
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Dec 2, 2025
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @vicky-dx, thank you for working on pytest. I think it wasn't actually tested the coverage metric would have gone up. How did you determine it wasn't covered ?

@jakkdl
Copy link
Member

jakkdl commented Dec 10, 2025

I think the coverage is coming from tests with RaisesGroup (and ~all of the regex handling is in the AbstractRaises base class)

def test_raisesexc_tostring() -> None:
assert str(RaisesExc(ValueError)) == "RaisesExc(ValueError)"
assert str(RaisesExc(match="[a-z]")) == "RaisesExc(match='[a-z]')"
pattern_no_flags = re.compile(r"noflag", 0)
assert str(RaisesExc(match=pattern_no_flags)) == "RaisesExc(match='noflag')"
pattern_flags = re.compile(r"noflag", re.IGNORECASE)
assert str(RaisesExc(match=pattern_flags)) == f"RaisesExc(match={pattern_flags!r})"
assert (
str(RaisesExc(ValueError, match="re", check=bool))
== f"RaisesExc(ValueError, match='re', check={bool!r})"
)

but that code is not doing end-to-end testing with compiled regexes, so seems fair enough to add tests that do it.

FTR I double-checked and raises.py does have 100% coverage across the file.

@The-Compiler
Copy link
Member

I think @vicky-dx might have meant "test coverage" in the (more loose) sense of the term, not in the sense of "number goes up".

Seems sensible to me!

@The-Compiler The-Compiler force-pushed the add-test-coverage-compiled-regex-match branch from 5d5db60 to c411c2b Compare December 11, 2025 14:49
@The-Compiler The-Compiler enabled auto-merge (squash) December 11, 2025 14:49
@The-Compiler The-Compiler merged commit 7eb7b3e into pytest-dev:main Dec 11, 2025
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants