pytest: No longer can extract message from exceptions in 5+ versions

I can’t extract the message of an exception using str(my_exception) when using pytest 5+. Here is an example :

E       AssertionError: assert 'blabla' in '<ExceptionInfo Exception tblen=1>'
E        +  where '<ExceptionInfo Exception tblen=1>' = str(<ExceptionInfo Exception tblen=1>)

That test succeeds in previous versions (4.6.4 for instance)

I’ve been able to test it using the following example :

import pytest

def test_ex():
    with pytest.raises(Exception) as e:
        raise Exception('blabla')

    assert 'blabla' in str(e)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (8 by maintainers)

Commits related to this issue

Most upvoted comments

@nicoddemus I understand the change but it did smash a good bit of my test suite out of the blue when my CI suite pulled v5 instead of v4.X. I didn’t realize I was using undocumented behavior which in the end is on me but I suspect several others will get hit in the face by this as well.

Maybe adding an extra note somewhere that statements such as assert "string" in str(err) will no longer work would help with the pain. I read the CHANGELOG prior to finding this issue but I didn’t realize the impact of #5412 until seeing it detailed out here.

Sorry, README was a typo, I meant CHANGELOG too.