Python“单元测试”模块根本无法工作

时间:2020-04-25 10:42:30

标签: python python-3.x unit-testing

我正在尝试在Python版本3中使用“ unittest”模块。

我试图验证其是否正确安装:

try:
   import unittest
   print("imported")
except ImportError as e:
   print("no")

该模块将正确导入,但是,如果我尝试访问其自己的方法,包括“ main()”和“ TestCase”,则会引发异常:

命令运行:python3 -m testunit.py

testunit.py:

    import unittest
import calculation



class TestCalc(unittest.TestCase):
    def test_add(self):
        result = calculation.add(10,50)
        self.assertEquals(result,60)

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

给出错误:

AttributeError: module 'unittest' has no attribute 'main'

其他结果:

如果我尝试运行命令 python3 -m unittest ,则应该给出输出“ RAN 0 tests in 0s”,但不会。

没有输出。

简而言之,该模块已正确安装,但似乎根本无法正常工作。

从错误中给出的Stacktrace:

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/Users/istorn/testunit.py", line 3, in <module>

0 个答案:

没有答案