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

assertRaises with context should replace context.exception with context.value #36

Open
graingert opened this issue Jun 21, 2019 · 6 comments

Comments

@graingert
Copy link
Member

input

with self.assertRaises(Exception) as ctx:
    sut()

self.assertIn("msg", str(ctx.exception))

expected output

with pytest.raises(Exception) as ctx:
    sut()

assert "msg" in str(ctx.value)
@graingert
Copy link
Member Author

graingert commented Jun 21, 2019

or maybe you could cheat and output

with pytest.raises(Exception) as ctx:
    sut()

ctx.exception = ctx.value # TODO: replace all uses of ctx.exception with ctx.value
assert "msg" in str(ctx.exception)

@htgoebel
Copy link
Collaborator

This would required static code analysis. I considert this as "won't fix", but will keep it open for the case someone else is eager to pick it up.

@graingert
Copy link
Member Author

Might be worth updating pytest to provide a .exception property alias

graingert added a commit to graingert/pytest that referenced this issue Jul 3, 2019
@graingert
Copy link
Member Author

@htgoebel I believe this is the smallest code change to fix this issue: pytest-dev/pytest#5541

graingert added a commit to graingert/pytest that referenced this issue Jul 3, 2019
graingert added a commit to graingert/pytest that referenced this issue Jul 3, 2019
For easier compatibility with unittest pytest-dev/unittest2pytest#36

Co-Authored-By: Bruno Oliveira <[email protected]>
graingert added a commit to graingert/pytest that referenced this issue Jul 3, 2019
For easier compatibility with unittest pytest-dev/unittest2pytest#36

Co-Authored-By: Bruno Oliveira <[email protected]>
@graingert
Copy link
Member Author

graingert commented Jul 3, 2019

Note: if your tests use the context managers ``with self.assertRaises`` or
``with self.assertWarns``, they will be transformed to ``pytest.raises`` or
``pytest.warns`` appropriately, but because the semantics are different, any
use of the output value from the context managers (e.g. the ``x`` in
``with pytest.raises(ValueError) as x:``) will be wrong and will require
manual adjustment after the fact.
should probably be updated for when pytest-dev/pytest#5541 lands

@jaraco
Copy link

jaraco commented Aug 1, 2022

I encountered this issue today. Did you consider emitting a warning when this condition is encountered?

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

3 participants