# unittest in Jupyter¶

In [1]:

    import unittest

In [12]:

    class MyTest(unittest.TestCase):
        def test1(self):
            """
            Test goes well
            """
            self.assertEqual(1+2,3)
        def test2(self):
            """
            Test goes also well
            """
            self.assertFalse(1+2==4)

In [13]:

    if __name__=='__main__':
        unittest.main()

    E
    ======================================================================
    ERROR: /Users/che/Library/Jupyter/runtime/kernel-8b325d38-8af7-4394-bf00-f1783883de04 (unittest.loader._FailedTest)
    ----------------------------------------------------------------------
    AttributeError: module '__main__' has no attribute '/Users/che/Library/Jupyter/runtime/kernel-8b325d38-8af7-4394-bf00-f1783883de04'

    ----------------------------------------------------------------------
    Ran 1 test in 0.001s

    FAILED (errors=1)

    An exception has occurred, use %tb to see the full traceback.

    SystemExit: True

    /usr/local/lib/python3.7/site-packages/IPython/core/interactiveshell.py:3339: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
      warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

You need to add a first argument, and avaoid a hard exit

In [14]:

    if __name__=='__main__':
        unittest.main(argv=['first-argument-is-ignored'],
                      exit=False,
                      verbosity=2
                     )

    test1 (__main__.MyTest) ... ok
    test2 (__main__.MyTest) ... ok

    ----------------------------------------------------------------------
    Ran 2 tests in 0.002s

    OK