19

I am giving an example which throws an error in ipython/jupyter notebook, but runs fine as an individual script.

import unittest

class Samples(unittest.TestCase):

    def testToPow(self):
        pow3 = 3**3
        assert pow3==27

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

The error is below:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-232db94ae8b2> in <module>()
      8 
      9 if __name__ == '__main__':
---> 10     unittest.main()

/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.pyc in __init__(self, module, defaultTest, argv, testRunner, testLoader, exit, verbosity, failfast, catchbreak, buffer)
     92         self.testLoader = testLoader
     93         self.progName = os.path.basename(argv[0])
---> 94         self.parseArgs(argv)
     95         self.runTests()
     96 

/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.pyc in parseArgs(self, argv)
    147             else:
    148                 self.testNames = (self.defaultTest,)
--> 149             self.createTests()
    150         except getopt.error, msg:
    151             self.usageExit(msg)

/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.pyc in createTests(self)
    156         else:
    157             self.test = self.testLoader.loadTestsFromNames(self.testNames,
--> 158                                                            self.module)
    159 
    160     def _do_discovery(self, argv, Loader=None):

/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.pyc in loadTestsFromNames(self, names, module)
    128         of string specifiers. See 'loadTestsFromName()'.
    129         """
--> 130         suites = [self.loadTestsFromName(name, module) for name in names]
    131         return self.suiteClass(suites)
    132 

/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.pyc in loadTestsFromName(self, name, module)
     98         obj = module
     99         for part in parts:
--> 100             parent, obj = obj, getattr(obj, part)
    101 
    102         if isinstance(obj, types.ModuleType):

AttributeError: 'module' object has no attribute '/Users/root3d/Library/Jupyter/runtime/kernel-c5225ac4-4b0e-4473-ae0a-3e051a704561'

What could be the possible problem? How should I write tests in notebook, then?

zondo
  • 19,901
  • 8
  • 44
  • 83
igauravsehrawat
  • 3,696
  • 3
  • 33
  • 46
  • Instead of `assert pow3==27` , consider using `self.assertEqual(pow3, 27)` and the other `assertXXX` methods the `unittest` module provides. – András Aszódi Nov 03 '20 at 06:34

3 Answers3

51

unittest.main looks at sys.argv by default, which is what started IPython, hence the error about the kernel connection file not being a valid attribute. You can pass an explicit list to main to avoid looking up sys.argv.

In the notebook, you will also want to include exit=False to prevent unittest.main from trying to shutdown the kernel process:

unittest.main(argv=['first-arg-is-ignored'], exit=False)

You can pass further arguments in the argv list, e.g.

unittest.main(argv=['ignored', '-v'], exit=False)
minrk
  • 37,545
  • 9
  • 92
  • 87
2

We can try TestLoader to load test cases from TestCaseClass

and attach those testcases to TextTestRunner then run it.

import unittest
suite = unittest.TestLoader().loadTestsFromTestCase(Samples)
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
Hari Baskar
  • 416
  • 7
  • 12
2

@Hari Baskar 's answer can be made generic by defining a decorator:

def run_test(tcls):
    """
    Runs unit tests from a test class
    :param tcls: A class, derived from unittest.TestCase
    """
    suite = unittest.TestLoader().loadTestsFromTestCase(tcls)
    runner = unittest.TextTestRunner(verbosity=2)
    runner.run(suite)

Run the test by applying the @run_test decorator to the class to be tested:

@run_test
class Samples(unittest.TestCase):
    def testToPow(self):
        pow3 = 3**3
        # use the tools the unittest module gives you...
        self.assertEqual(pow3, 27)
András Aszódi
  • 8,948
  • 5
  • 48
  • 51