python - 您如何使用 setup.cfg 正确限制 nosetests 中的 xcoverage 包覆盖范围?

标签 python code-coverage nosetests

我已对其进行配置,因此我的简单单元测试通过了。但是 nosexunit.xml 正在报告测试库文件。部分输出:

nosexcover-1.0.7-py2.6.egg/nosexcover/nosexcover    25     24     4%   5-41, 46-56
test/unit/test_setup                                13      0   100%   

该项目被分成需要独立测试的不同模块。我目前专注于后端模块。我想将覆盖范围限制在 lib 包中。项目树示例:

project
\-- backend     # <-- module I'm testing
    \-- lib     # <-- what I want to cover
    \-- test
       \-- unit/test_setup.py       # <-- test I'm running
    \-- setup.py
    \-- setup.cfg
\--reporting
    \-- setup.py
    \-- setup.cfg

我正在从名为 backend 的目录运行测试:

project/backend$ python setup.py nosetests -s --tests=unit/test_setup.py

nosetests在setup.cfg中配置如下

[nosetests]
# locating tests
where=./test
include=^unit.*

# coverage
cover-package=lib
cover-html=1
cover-html-dir=htmlcov
with-xcoverage=1
xcoverage-file=coverage.xml
with-xunit=1
xunit-file=nosexunit.xml
cover-erase=1

我感觉其中一个路径设置已关闭。我假设 wherecover-package 设置是相对于 setup.py 的位置(也是我运行测试的位置) 和 include 是相对于 where 的。

Nosetests 文档帮助不大。我希望有人可以直接告诉我。

最佳答案

事实证明,我毕竟已经正确配置了这个。问题似乎是,对于我过于简化的冒烟测试,我实际上并没有从我限制覆盖的模块中导入任何东西。

显然,在这种情况下,当覆盖率是零时,nosexcoverage 或 nosetests 不会报告那个无聊的事实,而是决定为您提供关于一堆其他内容的覆盖率报告。

通过为我想要覆盖的模块添加导入语句,我得到了正确的覆盖报告:

----------------------------------------------------------------------
XML: nosexunit.xml
Name        Stmts   Miss  Cover   Missing
-----------------------------------------
lib             0      0   100%   
lib.blank       1      0   100%   
-----------------------------------------
TOTAL           1      0   100%   
----------------------------------------------------------------------
Ran 2 tests in 0.008s

关于python - 您如何使用 setup.cfg 正确限制 nosetests 中的 xcoverage 包覆盖范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9676104/

相关文章:

windows - 免费的代码覆盖工具

python - Nose 测试有问题吗?使用 autospec=True 添加新案例将需要 15 秒

android - 适用于 Android 的 PyQt5 GUI 与 Kivy GUI

python - 替换文件中所有选定的字符

unit-testing - ReactJS:未经测试文件的 Jest 覆盖率报告显示覆盖率 0%

python - 使用 Pycharm 运行 Nosetests 并获得 "Empty test suite"

django - 如何使用 django-nose 运行单个测试或单个 TestCase?

python - 在破折号中将值从一页传递到另一页

python - 将列表和字典解压到同一参数中

swift - Xcode - Swift 计算属性的奇怪代码覆盖率