Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pytest.raises Assertion / Exception match parameter when there is a message in exception #217

Closed
azewiusz opened this issue Oct 20, 2023 · 1 comment

Comments

@azewiusz
Copy link
Contributor

Is your feature request related to a problem? Please describe.
Currently when cover reaches an exception either from assertion or thrown by code directly it is materialized in test in form of
statement like

EXAMPLE 1

def test_named_assertion_error():
    with pytest.raises(AssertionError):
        named_assertion_error(101)

It would be more informative if in case of "named" assertion we get match parameter in generated test like below:

EXAMPLE2

def test_named_assertion_error():
    with pytest.raises(AssertionError,
                       match='This is assertion error that should appear in test case match - second checkpoint'):
        named_assertion_error(101)

the above was generated without patch (EXAMPLE 1) as is now in version 0.0.42 and after applying patch (EXAMPLE2):

Code under test was:

def named_assertion_error(a: int):
    assert a > 100, "This is assertion error that should appear in test case match - first checkpoint"
    assert a > 160, "This is assertion error that should appear in test case match - second checkpoint"

Describe the solution you'd like

Example solution would be to amend path_cover.py file in few places - described in link below.
Patch is available here, may require review and unit tests extension.

Examples repository is here:
https://github.com/azewiusz/for-crosshair-tool/blob/main/example2/README.md

Describe alternatives you've considered
No clear alternatives, did this proposal to make certain analysis types easier.

@pschanely
Copy link
Owner

Thank you for the fix, @azewiusz!

I'll also include some of our gitter discussion about this feature here for a historical record:

I can imagine cases where the user would prefer the test case to not be tied to the exception message. It seems like Pynguin only checks the exception type. We could make the behavior configurable. But I think it's reasonable to simply always check the message. One way of thinking about it: the test generation is always more specific than you might want. It's easier to trim out the parts you don't want to check than to add them. And the same reasoning applies whether you're checking an exception or a returned data structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants