python 常见问题:导入py文件易忽略问题

  今天在python 中unittest 框架,编写测试用例时遇到个错误:TypeError: 'module' object is not callable。

此问题出现的场景为:testcase目录下的SeaCase.py中 

 class SeaCase(unittest.TestCase):

  def setUp(self):
     print('setup')
   def tearDown(self):
print('teardown')

def test_01_search(self):
     #其他代码

if __name__ == '__main__':
suit = unittest.TestSuite()
suit.addTest(SearchCase('test_01_search'))
#其他代码
runner.run(suit)
运行测试用例,测试用例是正常执行的。
将main函数单独取出,放置与testcase目录同级的main.py文件中
main.py
from testcase import SeaCase
if __name__ == '__main__':
suit = unittest.TestSuite()
suit.addTest(SeaCase('test_01_search'))
#其他代码
runner.run(suit)
出现问题。错误信息为:

suit.addTest(SeaCase('test_01_search'))
TypeError: 'module' object is not callable  

解决方法:

main.py 中导入使用

from testcase import SeaCase时, 意味着导入 SeaCase.py 文件,main中使用SeaCase类时,需:
SeaCase.SeaCase()

或者在main.py 中导入使用
from testcase.SeaCase import SeaCase
此时,代表导入的是 类 class SeaCase
在main中,使用类: SeaCase()

猜你喜欢

转载自www.cnblogs.com/MTXue/p/10420071.html