Skip to content
loading...

re: Asserting Exceptions with Pytest VIEW POST

FULL DISCUSSION
 

I know it's just an example but I think I should mention it for beginners: there are not many cases when raising Exception is the best strategy. The reason being that Exception is the class from which most error classes inherit from.

>>> Exception.__mro__
(<class 'Exception'>, <class 'BaseException'>, <class 'object'>)
>>> ValueError.__mro__
(<class 'ValueError'>, <class 'Exception'>, <class 'BaseException'>, <class 'object'>)

It would make it harder for the caller to properly handle different types of exceptions.

In your case ValueError (or a custom exception) is probably more appropriate:

Raised when an operation or function receives an argument that has the right type but an inappropriate value

A bonus tip: pytest.raises accepts an argument that you might find useful: match. You could rewrite:

 with pytest.raises(Exception) as e:
        assert check_email_format("bademail.com")
    assert str(e) == "Invalid email format"

as

with pytest.raises(Exception, match="Invalid email format"):
        assert check_email_format("bademail.com")

match is used to check the error message with a regular expression.

Hope this helps!

 

Yeah, it helps! Thanks for the input. It was just an example, of course in an ideal situation things would be different. I should have mentioned that.

Thanks again!

 

I just wanted to correct a common mistake in this comment since it was one of the first results from my google search. message is actually used for setting the message that pytest.rasies will display on failure. It's not about a comparison to the exception's message. match should always be used for that purpose. docs.pytest.org/en/latest/referenc...

(This problem catches almost everyone at one point or another. Which is the reason that pytest has chosen to deprecate the message parameter ;-)

 

thank you Toshio! I'll update the comment!

What did you search on Google to get here?

code of conduct - report abuse