是否可以从coverage.py 报告中排除测试目录?

发布于 2024-08-08 23:15:57 字数 434 浏览 8 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

追我者格杀勿论 2024-08-15 23:15:57

在项目根文件夹中创建 .coveragerc 文件,并包含以下内容:

[run]
omit = *tests*

Create .coveragerc file in your project root folder, and include the following:

[run]
omit = *tests*
溺ぐ爱和你が 2024-08-15 23:15:57

覆盖率 html --omit="*/test*" -d 测试/覆盖率

coverage html --omit="*/test*" -d tests/coverage

箜明 2024-08-15 23:15:57

如果任何 Django 开发人员需要为其项目提供 .coveragerc,请将其保留在此处。

[run]
source = .
omit = ./venv/*,*tests*,*apps.py,*manage.py,*__init__.py,*migrations*,*asgi*,*wsgi*,*admin.py,*urls.py

[report]
omit = ./venv/*,*tests*,*apps.py,*manage.py,*__init__.py,*migrations*,*asgi*,*wsgi*,*admin.py,*urls.py

在项目根目录中创建一个名为 .coveragerc 的文件,粘贴上面的代码,然后运行以下命令:

coverage run manage.py test

此外,如果您希望测试执行得更快,请运行此命令。

coverage run manage.py test --keepdb --parallel

这将保留测试数据库并并行运行测试。

Leaving this here in case if any Django developer needs a .coveragerc for their project.

[run]
source = .
omit = ./venv/*,*tests*,*apps.py,*manage.py,*__init__.py,*migrations*,*asgi*,*wsgi*,*admin.py,*urls.py

[report]
omit = ./venv/*,*tests*,*apps.py,*manage.py,*__init__.py,*migrations*,*asgi*,*wsgi*,*admin.py,*urls.py

Create a file named .coveragerc on your projects root directory, paste the above code and then just run the command:

coverage run manage.py test

In addition, if you want the tests to execute faster run this command instead.

coverage run manage.py test --keepdb --parallel

This will preserve the test DB and will run the tests in parallel.

菩提树下叶撕阳。 2024-08-15 23:15:57

您可以通过在项目根目录中创建 .coveragerc 来指定要排除的目录。

它支持通配符(您可以使用它来排除虚拟环境)注释(对于有效跟踪非常有用)。

下面的代码块显示了如何使用 omit (取自 最新文档)包含多个文件和目录。

[run]
omit =
    # omit anything in a .local directory anywhere
    */.local/*
    # omit everything in /usr
    /usr/*
    # omit this single file
    utils/tirefire.py

对于您的情况,您的 .coveragerc 中可能包含以下内容:

[run]
omit = 
    # ignore all test cases in tests/
    tests/*

对于有关覆盖率报告的问题,您可以按以下方式考虑测试和覆盖率:

  • 当您运行 pytest 时unittest,执行源代码的所有测试用例

  • 当您运行coverage时,它会显示该部分未使用的源代码。

  • 当您运行具有覆盖率的 pytest(类似于 pytest -v --cov)时,它会运行所有测试用例显示源代码中不存在的部分没有被使用。

额外

  • 您还可以在配置文件中指定 HTML 报告的位置,例如:
[html]
directory = tests/coverage/html_report/

这将创建 htmljs每次运行 coveragepytest -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.

[run]
omit =
    # omit anything in a .local directory anywhere
    */.local/*
    # omit everything in /usr
    /usr/*
    # omit this single file
    utils/tirefire.py

In your case, you could have the following in your .coveragerc:

[run]
omit = 
    # ignore all test cases in tests/
    tests/*

For your question on coverage reports, you can think about testing and coverage in the following manner:

  • When you run pytest or unittest, all the test cases for your source code are executed

  • When 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:

  • You can also specify the location of your HTML report in the configuration file like:
[html]
directory = tests/coverage/html_report/

This is going to create html, js, css, etc. inside tests/coverage/html_report/ everytime you run coverage or pytest -v --cov

铁轨上的流浪者 2024-08-15 23:15:57

您还可以明确指定哪个目录包含您想要覆盖的代码,而不是说明要省略哪些内容。在 .coveragerc 文件中,如果感兴趣的目录名为 demo,则如下所示

[run]
source = 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 called demo, this looks like

[run]
source = demo
苄①跕圉湢 2024-08-15 23:15:57

我遇到了同样的问题,并尝试了该线程上的所有回复。不想创建 .coveragerc 文件,我在这里找到了答案:
https://coverage.readthedocs.io/en/latest/source.html# source

你错过了星号:

coverage html --omit=tests/* -d tests/coverage

这对我有用 - 尽管我没有指定目录,所以我只是使用:

coverage html --omit=tests/*

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:

coverage html --omit=tests/* -d tests/coverage

this worked for me - although I don't specify a directory so I just use:

coverage html --omit=tests/*
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文