We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
When you use pytest, you sometimes need to cover code which might raise a specific exception. This can be done with the pytest.raises
helper function:
def test_start_tasks_db_raises():
"""Make sure unsupported db raises an exception."""
with pytest.raises(ValueError):
tasks.start_tasks_db('some/great/path', 'mysql')
However, in a lot of cases, testing the fact that an exception is raised is only half of the test. You probably want to check what exception message is thrown as well.
To do so, you can change your code to:
def test_start_tasks_db_raises():
"""Make sure unsupported db raises an exception."""
with pytest.raises(ValueError) as excinfo:
tasks.start_tasks_db('some/great/path', 'mysql')
exception_msg = excinfo.value.args[0]
assert exception_msg == "db_type must be a 'tiny' or 'mongo'"
When you store the exception, you can use value.args
to get to the actual exception message.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.