pytest-dev / unittest2pytest

helps rewriting Python `unittest` test-cases into `pytest` test-cases
GNU General Public License v3.0
128 stars 27 forks source link

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

Open graingert opened 5 years ago

graingert commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

graingert commented 5 years ago

@htgoebel I believe this is the smallest code change to fix this issue: https://github.com/pytest-dev/pytest/pull/5541

graingert commented 5 years ago

https://github.com/pytest-dev/unittest2pytest/blob/2c32cd693230d1b04de1d546d362d49fa891e7aa/README.rst#L78-L83 should probably be updated for when pytest-dev/pytest#5541 lands

jaraco commented 2 years ago

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