Skip to content

Instantly share code, notes, and snippets.

@hectorcanto
Last active March 28, 2019 14:02
Show Gist options
  • Save hectorcanto/e82d3edddb2b14c70ede4a41ee8a99fa to your computer and use it in GitHub Desktop.
Save hectorcanto/e82d3edddb2b14c70ede4a41ee8a99fa to your computer and use it in GitHub Desktop.
[Test exceptions] Testing that your program respond as expected in negative situations is very important.These tests exemplify how to check that some code raises the right Exception. #python #pytest #exceptions
"""
Testing that your program respond as expected in negative situations is very important.
These tests exemplify how to check that some code raises the right Exception.
"""
# TODO BreakingPoint exception
import pytest
def raise_exception():
raise KeyError("This function raises an exception")
# Note that you should use a message CONSTANT instead of a direct string
def test_raise_exception():
with pytest.raises(KeyError):
raise KeyError("Is expected")
with pytest.raises(KeyError):
raise_exception()
with pytest.raises(KeyError) as raised_exception:
raise_exception()
assert raised_exception.msg == "This function raises an exception."
@pytest.mark.xfail() # we expect this test to fail, just to prove the mechanism
def test_raise_unexpected_exception():
raise AttributeError
# It will add an xfail counter in the Result line
# something like: ========== 1 passed, 2 xfailed in 0.08 seconds =================
@pytest.mark.xfail(raises=KeyError)
def test_expected_other_exception():
"""
Some times something fails, you make a test but you cannot find a solution after many hours.
Instead of deleting the test for the suite to pass and forgetting about it; preserve the test,
mark it as xFail and tackle it in the future.
"""
with pytest.raises(AttributeError):
raise_exception()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment