无法在ipython / jupyter笔记本中运行unittest的主要功能

时间:2016-06-18 10:08:18

标签: python python-2.7 ipython-notebook jupyter-notebook python-unittest

我给出了一个在ipython / jupyter笔记本中抛出错误的示例,但作为单个脚本运行正常。

import unittest

class Samples(unittest.TestCase):

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

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

错误如下:

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

可能出现什么问题?那我该如何在笔记本上写测试呢?

3 个答案:

答案 0 :(得分:33)

unittest.main默认查看sys.argv,这是启动IPython的内容,因此内核连接文件的错误不是有效属性。您可以将显式列表传递给main以避免查找sys.argv。

在笔记本中,您还需要包含exit=False以防止unittest.main尝试关闭内核进程:

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

您可以在argv列表中传递更多参数,例如

unittest.main(argv=['ignored', '-v'], exit=False)

答案 1 :(得分:1)

我们可以尝试 TestLoader TestCaseClass

加载测试用例

并将这些测试用例附加到 TextTestRunner ,然后运行它。

import unittest
suite = unittest.TestLoader().loadTestsFromTestCase(Samples)
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)

答案 2 :(得分:1)

@Hari Baskar的答案可以通过定义装饰器而变得通用:

{
    "error": {
        "code": 403,
        "message": "The caller does not have permission",
        "status": "PERMISSION_DENIED"
    }
}

通过将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_test