Python 覆盖率:运行 超过 1 个测试
Python coverage: running more than 1 test
我想知道在使用 python 覆盖率配置 (.coveragerc
) 文件时,是否有一种方法可以为 运行 指定多个测试文件。
如果不是来自配置文件,也许从命令行 运行ning 时是可能的?
目前,我正在使用 3 个不同的单元测试文件:
coverage run test1
coverage run -a test2
coverage run -a test3
能再短点吗?
谢谢
编辑(2017-09-25): 正如@ned-batchelder 在评论中所说,如果开始一个新项目,更喜欢pytest over nose,因为鼻子没有维护.
通过查看 Coverage documentation,似乎 coverage
支持的唯一模式是 运行 使用每个命令指定一个特定模块。
您可以使用 nose pytest 等测试框架来 运行 所有测试,并报告 success/failure 率和总覆盖率。
使用 pytest
找出总的代码超量
1) 安装 pytest、coverage 和 pytest-cov
pip install pytest
pip install coverage
pip install pytest-cov
2) 运行 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
将找到匹配模式 test_*.py
(或其他,更多信息 here)的测试。
使用 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。
我想知道在使用 python 覆盖率配置 (.coveragerc
) 文件时,是否有一种方法可以为 运行 指定多个测试文件。
如果不是来自配置文件,也许从命令行 运行ning 时是可能的?
目前,我正在使用 3 个不同的单元测试文件:
coverage run test1
coverage run -a test2
coverage run -a test3
能再短点吗? 谢谢
编辑(2017-09-25): 正如@ned-batchelder 在评论中所说,如果开始一个新项目,更喜欢pytest over nose,因为鼻子没有维护.
通过查看 Coverage documentation,似乎 coverage
支持的唯一模式是 运行 使用每个命令指定一个特定模块。
您可以使用 nose pytest 等测试框架来 运行 所有测试,并报告 success/failure 率和总覆盖率。
使用 pytest
找出总的代码超量
1) 安装 pytest、coverage 和 pytest-cov
pip install pytest
pip install coverage
pip install pytest-cov
2) 运行 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
将找到匹配模式 test_*.py
(或其他,更多信息 here)的测试。
使用 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。