简体   繁体   English

用于单元测试的python3:AttributeError:模块'__main__'没有属性“内核......”

[英]python3 for unit test: AttributeError: module '__main__' has no attribute “kernel…”

I am practicing the unittest for python and it always directed to a JSON file:我正在练习 python 的单元测试,它总是指向一个 JSON 文件:

#unittest code# import unittest from name_function import get_formatted_name class NamesTestCase(unittest.TestCase): #unittest code# 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()

ERROR: /Users/xxx/Library/Jupyter/runtime/kernel-0eacd257-bf93-4c0f-843c-2e8a96377a17 (unittest.loader._FailedTest)错误:/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' AttributeError: 模块“ main ”没有属性“/Users/xxx/Library/Jupyter/runtime/kernel-0eacd257-bf93-4c0f-843c-2e8a96377a17”

the traceback is:回溯是:

 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

How do I fix this?我该如何解决这个问题?

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

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

using following:使用以下:

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

It worked, but was failing before due to a mirror typo.它有效,但之前由于镜像错别字而失败。 I replaced:我替换了:

self.asertEqual

with:与:

self.assertEqual

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:模块'__main__'没有属性'cleaner' - AttributeError: module '__main__' has no attribute 'cleaner' AttributeError: 模块 '__main__' 没有属性 'AverageWordLengthExtractor' - AttributeError: module '__main__' has no attribute 'AverageWordLengthExtractor' pickle/joblib AttributeError: 模块 &#39;__main__&#39; 在 pytest 中没有属性 &#39;thing&#39; - pickle/joblib AttributeError: module '__main__' has no attribute 'thing' in pytest unittest:AttributeError:模块'__main__'没有属性'C:\ ...' - unittest: AttributeError: module '__main__' has no attribute 'C:\…' Python多处理错误:AttributeError:模块&#39;__main__&#39;没有属性&#39;__spec__&#39; - Python Multiprocessing error: AttributeError: module '__main__' has no attribute '__spec__' TensorFlow:模块&#39;__main__&#39;没有属性&#39;main&#39; - TensorFlow: module '__main__' has no attribute 'main' 查找“pip”的模块规范时出错(AttributeError:模块“__main__”没有属性“__file__”) - Error while finding module specification for 'pip' (AttributeError: module '__main__' has no attribute '__file__') Python-&gt; AttributeError:“模块”对象没有属性“主” - Python -> AttributeError: 'module' object has no attribute 'main' Pydoop mapreduce“ AttributeError:模块&#39;wordcount_minimal&#39;没有属性&#39;__main__&#39;” - Pydoop mapreduce “AttributeError: module 'wordcount_minimal' has no attribute '__main__'” Python 多处理错误:AttributeError: Can't get attribute 'task' on<module '__main__' (built-in)> "</module> - Python Multiprocessing error: AttributeError: Can't get attribute 'task' on <module '__main__' (built-in)>"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM