Python覆盖率:运行1个以上的测试 [英] Python coverage: running more than 1 test
问题描述
我想知道是否有一种方法可以指定使用python coverage配置( .coveragerc
)文件时要运行的多个测试文件。
如果不是从配置文件中获取,也许从命令行运行时可能吗?
目前,我正在使用3个不同的单元测试文件:
I'm wondering is there a way to specify more than 1 test file to run when using python coverage config (.coveragerc
) file.
If not from a config file, maybe it is possible when running from command line?
At the moment, for 3 different unit tests files I'm using:
coverage run test1
coverage run -a test2
coverage run -a test3
可以短一些吗?
谢谢
Can it be any shorter? Thanks
推荐答案
编辑(2017-09-25):如@ ned- batchelder在评论中表示,与 pytest .readthedocs.io / zh-CN / latest / rel = nofollow noreferrer>鼻子如果开始新项目,因为鼻子无法保持。
Edit (2017-09-25): As @ned-batchelder says in the comments, prefer pytest over nose if starting a new project, as nose is unmaintained.
看看封面文档,看起来 coverage
支持的模式是每个命令都运行一个特定的模块。
By taking a look at Coverage documentation, it looks like that the only mode that coverage
supports is running a specific module with each command.
您可以使用测试框架,例如 鼻子 pytest ,以运行所有测试,并报告成功/失败率和总覆盖率。
You could use a testing framework, such as nose pytest, to run all your tests, and report the success/failure rate and the total coverage.
1)安装pytest,coverage和pytest-cov
1) Install pytest, coverage, and pytest-cov
pip install pytest
pip install coverage
pip install pytest-cov
2)使用-cov $ c $运行
pytest
命令c>标记每个需要测量覆盖率的模块或软件包。例如:
2) Run the pytest
commmand, using the --cov
flag for every module or package whose coverage you need measured. For example:
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%
<$ c如果$ c> pytest 与模式 test _ *。py
(或其他,更多信息此处)。
pytest
will find your tests if they match the pattern test_*.py
(or others, more info here).
< s>
1)安装鼻子和覆盖范围
1) Install nose and coverage
pip install nose
pip install coverage
2)运行 nosetests
命令,并使用- -with-coverage
标志
2) Run the nosetests
command, with the --with-coverage
flag
nosetests --with-coverage
样本输出(当使用单个模块foo.py时) :
Sample output (when having a single module foo.py):
Name Stmts Miss Cover
----------------------------
foo.py 6 2 67%
----------------------------------------------------------------------
Ran 1 test in 0.008s
OK
鼻子测试
可以使用启发式方法自动找到您的测试。例如,如果将测试放在以 test
开头的文件名中,并通过继承 unittest.TestCase
, noestests
会找到它们。更多信息此处。
nosetests
can automatically find your tests using some heuristics. For example, if you put your tests in filenames that start with test
, and create your testcases by inheriting from unittest.TestCase
, nosetests
will find them. More info here.
这篇关于Python覆盖率:运行1个以上的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!