Expecting Exceptions

  • The test PASSes if the function we test will raise a TypeError.
    • If no exception is raised, the test fails. If the test raises a different exception, it fails.
def test_add_raises():
	with pytest.raises(TypeError):
		tasks.add(task='not a Task object')

Full example

def test_start_tasks_db_wrong_path():
"""Make sure unsupported db raises an exception."""
	with pytest.raises(ValueError) as excinfo:
        tasks.start_tasks_db('some/great/path', 'hdfs')
  exception_msg = excinfo.value.args[0]
  assert excinfo.type is ValueError
  assert exception_msg == "db_type must be a 'tiny' or 'mongo'"
SuperMade with Super