python - 在 python 中显示覆盖率的命令行选项

标签 python unit-testing nose test-coverage python-coverage

当我使用nose时从终端显示具有覆盖范围的测试结果,我收到错误和实际覆盖的缺失行

$ python -m Nose.core test_utils.py -s --with-coverage

======================================================================
ERROR: Failure: AttributeError ('module' object has no attribute 'test_utils')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/nose/loader.py", line 413, in loadTestsFromName
    addr.filename, addr.module)
  File "/Library/Python/2.7/site-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/Library/Python/2.7/site-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/Development/python/slideShow/tests/test_utils.py", line 50, in <module>
    unittest.main()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 94, in __init__
    self.parseArgs(argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 149, in parseArgs
    self.createTests()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 158, in createTests
    self.module)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 128, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
    parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'test_utils'

Name      Stmts   Miss  Cover   Missing
---------------------------------------
PyQt4         0      0   100%   
getopt      103    103     0%   34-210
mox         597    597     0%   17-1898
stubout      50     50     0%   17-142
utils        23     16    30%   8-10, 13-20, 23, 26-30

怎么没有找到test_utils?我站在同一个目录中!

最佳答案

好吧,在排除故障时我得到了答案,

测试模块必须有

如果 __name__ == '__main__': 单元测试.main()

为了让python -m noose.core test_utils.TestUtils --with-coverage工作

早些时候我刚刚有unittest.main()

关于python - 在 python 中显示覆盖率的命令行选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20160983/

相关文章:

python - 从 Python 中的路径确定文件系统类型

Python - 多处理错误 'cannot start a process twice'

java - 在单元测试中模拟 RxJava 的 Activity 生命周期

java - 测试我的 Maven 库与 Android 的兼容性

python - 如何在 Python 模拟中调用模拟方法

python - 为 pyspark 运行 nosetests

python - 运行生成的 Nose 测试

python - 根据列数重构 pandas 数据框

python - 管道不适用于标签编码器

windows - 属性错误 : StringIO instance has no attribute 'encoding'