zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    call 与 apply
    react-router
    阻止IOS上的弹性滚动
    React规范
    sessionStorage 、localStorage 和 cookie 对比区分
    显式Intent 和隐式 Intent 的区别
    Activity之间传递数据的方式及常见问题总结
    Android横竖屏切换生命周期变化
    String、StringBuilder、StringBuffer 区别
    内存泄漏
  • 原文地址:https://www.cnblogs.com/MTXue/p/10420071.html
Copyright © 2011-2022 走看看