我是iPython的新手,正在尝试帮助另一位开发人员入门,我们都遇到了相同的问题。
我们正在尝试从https://docs.python.org/2/library/unittest.html#basic- example在iPython中运行python单元测试示例, 该代码在Windows和ubuntu上的命令行上运行得很好,没有任何修改来自iPy笔记本的代码生成以下异常:
AttributeError: 'module' object has no attribute '/home/myuser/'
文件名是: /home/myuser/example_unittest.ipynb
/home/myuser/example_unittest.ipynb
到目前为止,我没有碰运气的iPython文档和谷歌。任何调试技巧,或解决此问题的线索,不胜感激。
(全栈):
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()
为了在ipython笔记本中运行if __name__ == '__main__'单元测试,请删除部分代码,然后在新的单元格中创建测试套件,然后使用来运行它TextTestRunner,
if __name__ == '__main__'
TextTestRunner
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions) unittest.TextTestRunner().run(suite)