用于单元测试的python3:AttributeError:module' __ main __'没有属性"内核..."

时间:2018-04-21 04:22:14

标签: python python-unittest

我正在为python练习unittest,它总是指向一个JSON文件:

unittest代码

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()
  

错误:   /用户/ XXX /库/ Jupyter /运行/内核0eacd257-bf93-4c0f-843℃,2e8a96377a17   (unittest.loader._FailedTest)

     

AttributeError:module' main '没有属性   ' /用户/ XXX /库/ Jupyter /运行/内核0eacd257-bf93-4c0f-843℃-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
&#13;
&#13;
&#13;

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

使用以下内容:

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

它起作用了,但是由于镜像错字而失败了。我替换:

self.asertEqual

具有:

self.assertEqual