如何使覆盖范围包括未测试的文件? [英] How do I make coverage include not tested files?

查看:25
本文介绍了如何使覆盖范围包括未测试的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始为使用 unittestcoverage 的 Python 项目编写一些单元测试.我目前只测试一小部分,但我正在尝试计算代码覆盖率

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

我运行我的测试并使用以下方法获得覆盖率

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

我遇到的问题是 coverage 告诉我有 44% 的代码覆盖率并且只计算以下文件:

The problem I'm having is that coverage is telling I have 44% code coverage and is only counting the files that:

  1. 在单元测试中进行了测试(即,所有未测试的文件都缺失且不在整体覆盖范围内)

  1. 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?

推荐答案

--source=. 添加到 coverage 运行行.它既会将焦点限制在当前目录上,也会搜索根本没有运行的 .py 文件.

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.

这篇关于如何使覆盖范围包括未测试的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆