Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError when running unittest sample in iPy Notebook

I am new to iPython and trying to help another developer get started and we are both hitting same issues.

We are attempting to run a python unittest sample in iPython from https://docs.python.org/2/library/unittest.html#basic-example The code runs just fine from command line on windows and ubuntu without ANY modifications Exact same code from iPy notebook generates following exception:

AttributeError: 'module' object has no attribute '/home/myuser/'

The filename is: /home/myuser/example_unittest.ipynb

I have noodled the iPython docs and google with no luck as of the moment. Any debugging tips, or clues to solving this issue are appreciated.

(full stack):

AttributeError                            Traceback (most recent call last)
<ipython-input-2-39bc0ec16f11> in <module>()
     28 
     29 if __name__ == '__main__':
---> 30     unittest.main()
     31 

/usr/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/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/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=loader.TestLoader):

/usr/lib/python2.7/unittest/loader.pyc in loadTestsFromNames(self, names, module)
    126         of string specifiers. See 'loadTestsFromName()'.
    127         """
--> 128         suites = [self.loadTestsFromName(name, module) for name in names]
    129         return self.suiteClass(suites)
    130 

/usr/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 '/home/myuser/'
like image 890
mb-texas Avatar asked Jul 28 '14 21:07

mb-texas


2 Answers

unittest.main() is primarily for command line execution.

In order to run a unittest in the ipython notebook, remove the if __name__ == '__main__' part of the code and, in a new cell, create a test suite and then run it using TextTestRunner,

suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
unittest.TextTestRunner().run(suite)
like image 90
Gabriel Avatar answered Oct 08 '22 18:10

Gabriel


The reason you are getting that error because of unittest.main checks for arguments (sys.argv). It's what starts iPython or Jupyter.

Change your code to:

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

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

like image 26
Vlad Bezden Avatar answered Oct 08 '22 17:10

Vlad Bezden