是否可以从coverage.py 报告中排除测试目录?
我是Python单元测试的新手,尤其是coverage.py。覆盖率报告是否需要包含实际测试文件的覆盖率?
以下是我的 HTML 报告 的屏幕截图作为示例。
您可以看到报告包含 tests/test_credit_card
。起初,我试图从报告中省略 tests/
目录,如下所示:
coverage html --omit=tests/ -d tests/coverage
我尝试了该命令的几种变体,但我终其一生都无法得到测试/排除。接受失败后,我开始怀疑测试文件是否应该包含在报告中。
有人能解释一下吗?
I'm kind of a rookie with python unit testing, and particularly coverage.py. Is it desirable to have coverage reports include the coverage of your actual test files?
Here's a screenshot of my HTML report as an example.
You can see that the report includes tests/test_credit_card
. At first I was trying to omit the tests/
directory from the reports, like so:
coverage html --omit=tests/ -d tests/coverage
I tried several variations of that command but I could not for the life of me get the tests/ excluded. After accepting defeat, I began to wonder if maybe the test files are supposed to be included in the report.
Can anyone shed some light on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在项目根文件夹中创建 .coveragerc 文件,并包含以下内容:
Create
.coveragerc
file in your project root folder, and include the following:覆盖率 html --omit="*/test*" -d 测试/覆盖率
coverage html --omit="*/test*" -d tests/coverage
如果任何 Django 开发人员需要为其项目提供 .coveragerc,请将其保留在此处。
在项目根目录中创建一个名为 .coveragerc 的文件,粘贴上面的代码,然后运行以下命令:
此外,如果您希望测试执行得更快,请运行此命令。
这将保留测试数据库并并行运行测试。
Leaving this here in case if any Django developer needs a .coveragerc for their project.
Create a file named .coveragerc on your projects root directory, paste the above code and then just run the command:
In addition, if you want the tests to execute faster run this command instead.
This will preserve the test DB and will run the tests in parallel.
您可以通过在项目根目录中创建
.coveragerc
来指定要排除的目录。它支持通配符(您可以使用它来排除虚拟环境)和注释(对于有效跟踪非常有用)。
下面的代码块显示了如何使用
omit
(取自 最新文档)包含多个文件和目录。对于您的情况,您的
.coveragerc
中可能包含以下内容:对于有关覆盖率报告的问题,您可以按以下方式考虑测试和覆盖率:
当您运行
pytest 时
或unittest
,执行源代码的所有测试用例当您运行
coverage
时,它会显示该部分未使用的源代码。当您运行具有覆盖率的 pytest(类似于 pytest -v --cov)时,它会运行所有测试用例并显示源代码中不存在的部分没有被使用。
额外:
这将创建 html、js、每次运行
coverage
或pytest -v --cov
时,在tests/coverage/html_report/
内 >css 等You can specify the directories you want to exclude by creating a
.coveragerc
in the project root.It supports wildcards (which you can use to exclude virtual environment) and comments (very useful for effective tracking).
The below code block shows how
omit
can be used (taken from the latest documentation) with multiple files and directories.In your case, you could have the following in your
.coveragerc
:For your question on coverage reports, you can think about testing and coverage in the following manner:
When you run
pytest
orunittest
, all the test cases for your source code are executedWhen you run
coverage
, it shows the part of the source code that isn't being used.When you run pytest with coverage (something like
pytest -v --cov
), it runs all test cases and shows the part of the source code that isn't being used.Extra:
This is going to create html, js, css, etc. inside
tests/coverage/html_report/
everytime you runcoverage
orpytest -v --cov
您还可以明确指定哪个目录包含您想要覆盖的代码,而不是说明要省略哪些内容。在
.coveragerc
文件中,如果感兴趣的目录名为demo
,则如下所示You can also explicitly specify which directory has the code you want coverage on instead of saying which things to omit. In a
.coveragerc
file, if the directory of interest is calleddemo
, this looks like我遇到了同样的问题,并尝试了该线程上的所有回复。不想创建 .coveragerc 文件,我在这里找到了答案:
https://coverage.readthedocs.io/en/latest/source.html# source
你错过了星号:
这对我有用 - 尽管我没有指定目录,所以我只是使用:
I had the same issue and tried every response on this thread. Not wanting to create a .coveragerc file, I found the answer here:
https://coverage.readthedocs.io/en/latest/source.html#source
you're missing the asterisk:
this worked for me - although I don't specify a directory so I just use: