如何进行覆盖包括未测试的文件?

时间:2021-12-24 01:58:38

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

我刚开始为使用unittest和coverage的python项目编写一些单元测试。我目前只测试一小部分,但我正在努力解决代码覆盖问题

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:

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

  1. were tested in the unit tests (i.e., all the files that were not tested are missing and not in the overall coverage)

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

  2. 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)

此外,它表示在这些单元测试中实际测试的文件只测试了前几行(在大多数情况下是import语句)

How do I get a more realistic code coverage or is this how it is meant to be?

我如何获得更实际的代码覆盖率,或者它是如何实现的?

2 个解决方案

#1


26  

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 =。到覆盖运行线。它会将焦点限制在当前目录中,并将搜索根本不运行的.py文件。

#2


1  

If you use nose as a testrunner instead, the coverage plugin for it provides

如果你使用nose作为testrunner,它的coverage插件提供

  --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]

#1


26  

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 =。到覆盖运行线。它会将焦点限制在当前目录中,并将搜索根本不运行的.py文件。

#2


1  

If you use nose as a testrunner instead, the coverage plugin for it provides

如果你使用nose作为testrunner,它的coverage插件提供

  --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]