Title: Test For A Specific Exception
Slug: test_for_a_specific_exception
Summary: Using Python.
Date: 2017-02-02 12:00
Category: Python
Tags: Testing
Authors: Chris Albon

Preliminaries


In [1]:
import unittest

Create A Function To Test


In [2]:
def add(x, y):
    return x + y

Create Test Case


In [3]:
# Create a test case
class TestAdd(unittest.TestCase):
    # Create the unit test
    def test_input_string(self):
        # Test To make sure a TypeError exception is raised
        self.assertRaises(TypeError, add('Banana', 'Boat'))

Run Test


In [4]:
# Run the unit test (and don't shut down the Jupyter Notebook)
unittest.main(argv=['ignored', '-v'], exit=False)


test_input_string (__main__.TestAdd) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.002s

OK
Out[4]:
<unittest.main.TestProgram at 0x104855320>