Python覆盖:运行超过1个测试

时间:2017-09-24 09:11:23

标签: python code-coverage

我想知道有没有办法在使用python coverage config(.coveragerc)文件时指定运行多个测试文件。 如果不是来自配置文件,也许从命令行运行时可能? 目前,我正在使用3个不同的单元测试文件:

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

它可以更短吗? 感谢

1 个答案:

答案 0 :(得分:1)

编辑(2017-09-25):正如@ n​​ed-batchelder在评论中所说,如果开始一个新项目,首选pytest而不是nose,因为鼻子是没有维护。

通过查看Coverage documentation,看起来this.categoryForm.get('parent_id').setValue(e.item.parent_id); 支持的唯一模式是使用每个命令运行特定模块。

您可以使用测试框架(例如 nose pytest)来运行所有测试,并报告成功/失败率和总覆盖率。

使用coverage

查找总代码超额

1)安装pytest,coverage和pytest-cov

pytest

2)运行pip install pytest pip install coverage pip install pytest-cov 命令,对于需要测量其覆盖范围的每个模块或包,使用pytest标志。例如:

--cov

示例输出:

pytest --cov=foo --cov=bar

Name Stmts Miss Cover Missing -------------------------------------- bar.py 3 1 67% 5 foo.py 6 2 67% 9-11 -------------------------------------- TOTAL 9 3 67% 如果匹配模式pytest(或其他人,更多信息here),则会找到您的测试。

使用test_*.py

查找总代码覆盖率

1)安装鼻子和覆盖物

nose

2)使用pip install nose pip install coverage 标志

运行nosetests命令
--with-coverage

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

nosetests --with-coverage

Name Stmts Miss Cover ---------------------------- foo.py 6 2 67% ---------------------------------------------------------------------- Ran 1 test in 0.008s OK 可以使用一些启发式方法自动找到您的测试。例如,如果您将测试放在以nosetests开头的文件名中,并通过继承test来创建测试用例,则unittest.TestCase会找到它们。更多信息here