2017-09-24 49 views
0

我想知道是否有一种方法来指定使用python覆盖配置(.coveragerc)文件时运行的多个测试文件。 如果不是从配置文件,也许有可能从命令行运行时? 目前,3个不同的单元测试文件,我使用的是:Python的覆盖范围:运行超过1个测试

coverage run test1 
coverage run -a test2 
coverage run -a test3 

它可以是任何短? 感谢

+0

您可以通过pytest运行覆盖范围... – thebjorn

回答

1

编辑(2017年9月25日):作为@ NED-尔德在评论中说,宁可pytest超过nose如果开始一个新项目,因为鼻子是无人维护。

通过看看Coverage documentation,它看起来像coverage支持的唯一模式是使用每个命令运行特定的模块。

你可以使用一个测试框架,如nosepytest,运行所有测试,并报告成功/失败率和全覆盖。

使用pytest

1)安装pytest,覆盖范围和pytest-COV

pip install pytest 
pip install coverage 
pip install pytest-cov 

2)执行pytest条命令找出总代码超龄,使用--cov标志的每一个模块或程序包,其您需要测量的覆盖率。例如:

pytest --cov=foo --cov=bar 

输出示例:如果他们匹配的模式test_*.py(或其他人,更多信息here

Name  Stmts Miss Cover Missing 
-------------------------------------- 
bar.py  3  1 67% 5 
foo.py  6  2 67% 9-11 
-------------------------------------- 
TOTAL  9  3 67% 

pytest会发现你的测试。

使用nose

1)安装鼻和覆盖

pip install nose 
pip install coverage 

2)运行nosetests命令与--with-coverage标志

nosetests --with-coverage 
找出总代码覆盖

示例输出(具有单个模块foo.py时):

Name  Stmts Miss Cover 
---------------------------- 
foo.py  6  2 67% 
---------------------------------------------------------------------- 
Ran 1 test in 0.008s 

OK 

nosetests可以使用一些启发你的测试自动发现。例如,如果您将测试放在以test开头的文件名中,并通过继承unittest.TestCase来创建测试用例,那么nosetests将找到它们。更多信息here

+1

请勿使用鼻子。它没有维护。现在从pytest开始。 –

相关问题