python3用于单元测试: AttributeError: module '__main__' has no attribute "kernel..."

标签 python jupyter python-unittest

我正在练习 python 的单元测试,它总是定向到 JSON 文件:

单元测试代码

import unittest
from name_function import get_formatted_name
class NamesTestCase(unittest.TestCase):

    def test_first_last_name(self):
        formatted_name=get_formatted_name("allen","park")
        self.assertEqual(formatted_name,"Allen Park")

unittest.main()

错误

/Users/xxx/Library/Jupyter/runtime/kernel-0eacd257-bf93-4c0f-843c-2e8a96377a17
(unittest.loader._FailedTest)

AttributeError: module '__main__' has no attribute
'/Users/xxx/Library/Jupyter/runtime/kernel-0eacd257-bf93-4c0f-843c-2e8a96377a17'

回溯是:

    SystemExit                                Traceback (most recent call last)
    <ipython-input-1-15fe4fc5728d> in <module>()
         20         self.assertIn(response,my_survey.responses)
         21 
    ---> 22 unittest.main()

    /anaconda3/lib/python3.6/unittest/main.py in __init__(self, module, defaultTest, argv, testRunner, testLoader, exit, verbosity, failfast, catchbreak, buffer, warnings, tb_locals)
         93         self.progName = os.path.basename(argv[0])
         94         self.parseArgs(argv)
    ---> 95         self.runTests()
         96 
         97     def usageExit(self, msg=None):

    /anaconda3/lib/python3.6/unittest/main.py in runTests(self)
        256         self.result = testRunner.run(self.test)
        257         if self.exit:
    --> 258             sys.exit(not self.result.wasSuccessful())
        259 
        260 main = TestProgram

    SystemExit: True

如何解决这个问题?

最佳答案

看看“https://medium.com/@vladbezden/using-python-unittest-in-ipython-or-jupyter-732448724e31

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

关于python3用于单元测试: AttributeError: module '__main__' has no attribute "kernel...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49952317/

相关文章:

AWS Lambda 中的 Python 没有正确垃圾收集?

debugging - 有没有办法跳转到 iPython/Jupyter 笔记本中包含错误的行?

python - 使用 jupyter 小部件来保存对 pandas 数据框的点击

python - pytest 不会使用 mock.patch 引发 HTTPError

python - 为什么 unittest.Test Cases 看不到我的 pytest fixtures?

python - pow() 提升为 float

python - 如何为 Keras/tf.Keras 构建自定义数据生成器,其中 X 图像被增强并且对应的 Y 标签也是图像

python - 如何从 ctypes 结构或联合字段中获取 'type' 字段描述符

ubuntu - Jupyter notebook 不会自动加载到浏览器中

python - 在 Nose 测试中打印不同的长描述以及测试名称 python