免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5095 | 回复: 1
打印 上一主题 下一主题

求救,unittest无法执行,Ran 0 tests in 0.000s [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-04-13 11:35 |只看该作者 |倒序浏览
本帖最后由 kkmm0105 于 2014-04-13 11:52 编辑

按照网上了一篇文章写了unittest程序,运行结果:
Ran 0 tests in 0.000s     为啥是0 test呢? 代码如下:

主函数main.py
  1. import re
  2. import os
  3. import sys
  4. import unittest


  5. def test_all_in_current():
  6.     files = os.listdir("./lib/workers/ut/")
  7.     test = re.compile("test.*\.py$", re.IGNORECASE)
  8.     case_files = filter(test.search, files)

  9.     filename_to_modulename = lambda f: "workers.ut." + os.path.splitext(f)[0]
  10.     module_names = map(filename_to_modulename, case_files)
  11.     modules = map(__import__, module_names)

  12.     load = unittest.defaultTestLoader.loadTestsFromModule
  13.     return unittest.TestSuite(map(load, modules))

  14. if __name__ == "__main__":
  15.     libpath = "./lib/"
  16.     sys.path.insert(0, libpath)
  17.     unittest.main(defaultTest="test_all_in_current")
复制代码
测试类TestCar.py,位于./lib/workers/ut/
  1. class TestCar(unittest.TestCase):
  2.    
  3.     def setUp(self):
  4.         print 'sdfsdf'

  5.     def tearDown(self):
  6.         pass

  7.     def test_opendoor(self):
  8.         print 'open'
复制代码
从debug信息上看,__import__函数已经成功了,那问题出在哪呢

论坛徽章:
0
2 [报告]
发表于 2014-04-18 01:17 |只看该作者
__import__的问题。

应该这样:
import functools
modules = map(functools.partial(__import__, fromlist=[None,]), modules_names)

查看官方文档
https://docs.python.org/2/library/functions.html#__import__

__import__(name[, globals[, locals[, fromlist[, level]]]])
When the name variable is of the form package.module, normally, the top-level package (the name up till the first dot) is returned, not the module named by name.
However, when a non-empty fromlist argument is given, the module named by name is returned.

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP