本文介绍了在iPy Notebook中运行unittest示例时出现AttributeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPython的新手,正在尝试帮助另一位开发人员入门,我们都遇到了相同的问题.

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

我们正尝试从 https:在iPython中运行python单元测试示例: //docs.python.org/2/library/unittest.html#basic-example 该代码可以在Windows和ubuntu上从命令行正常运行,而无需进行任何修改来自iPy Notebook的完全相同的代码会生成以下异常:

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

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

文件名是:/home/myuser/example_unittest.ipynb

目前为止,我没有为iPython文档和google做好准备.任何调试技巧或解决此问题的线索都将受到赞赏.

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.

(完整堆栈):

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/'

推荐答案

unittest.main()主要用于命令行执行.

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

为了在ipython笔记本中运行单元测试,请删除代码的if __name__ == '__main__'部分,并在新的单元格中创建一个测试套件,然后使用TextTestRunner

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)

这篇关于在iPy Notebook中运行unittest示例时出现AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 12:31