Python 如何使覆盖范围包括未测试的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34728734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make coverage include not tested files?
提问by
I have just started writing some unit tests for a python project I have using unittest
and coverage
. I'm only currently testing a small proportion, but I am trying to work out the code coverage
我刚刚开始为我使用的 python 项目编写一些单元测试unittest
和coverage
。我目前只测试一小部分,但我正在尝试计算代码覆盖率
I run my tests and get the coverage using the following
我运行我的测试并使用以下方法获得覆盖率
python -m unittest discover -s tests/
coverage run -m unittest discover -s tests/
coverage report -m
The problem I'm having is that coverage
is telling I have 44% code coverage and is only counting the files that:
我遇到的问题是,coverage
我有 44% 的代码覆盖率,并且只计算以下文件:
were tested in the unit tests (i.e., all the files that were not tested are missing and not in the overall coverage)
were in the libraries in the virtual environment and code coverage of the actual tests too. Surely it should not be including the actual tests in the results?
在单元测试中进行了测试(即,所有未测试的文件都丢失并且不在整体覆盖范围内)
在虚拟环境中的库和实际测试的代码覆盖率中。当然,它不应该在结果中包含实际测试?
Furthermore, it says the files that are actually tested in these unit tests only have the first few lines tested (which are in most cases the import statements)
此外,它说在这些单元测试中实际测试的文件只测试了前几行(在大多数情况下是导入语句)
How do I get a more realistic code coverage or is this how it is meant to be?
我如何获得更现实的代码覆盖率,或者这就是它的本意?
采纳答案by Ned Batchelder
Add --source=.
to the coverage
run line. It will both limit the focus to the current directory, and will search for .py
files that weren't run at all.
添加--source=.
到coverage
运行行。它既会将焦点限制在当前目录上,又会搜索.py
根本没有运行的文件。
回答by Daenyth
If you use nose
as a testrunner instead, the coverage plugin for it provides
如果您nose
改为用作测试运行程序,则它的覆盖率插件提供
--cover-inclusive Include all python files under working directory in
coverage report. Useful for discovering holes in test
coverage if not all files are imported by the test
suite. [NOSE_COVER_INCLUSIVE]
--cover-tests Include test modules in coverage report
[NOSE_COVER_TESTS]