unittest2pytest icon indicating copy to clipboard operation
unittest2pytest copied to clipboard

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

Open graingert opened this issue 6 years ago • 6 comments

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 avatar Jun 21 '19 16:06 graingert

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)

graingert avatar Jun 21 '19 16:06 graingert

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.

htgoebel avatar Jun 30 '19 10:06 htgoebel

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

graingert avatar Jun 30 '19 11:06 graingert

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

graingert avatar Jul 03 '19 09:07 graingert

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

graingert avatar Jul 03 '19 11:07 graingert

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

jaraco avatar Aug 01 '22 01:08 jaraco