running nose——覆盖所有包文件,但不包括其他依赖项和lib

2024-06-06 14:33:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我的项目文件夹(是的-我知道这是最佳实践)类似于:

.
├── app.py
├── otherscript.py
├── tests/
└── tools/
    ├── __init__.py
    └── toolfile.py

我需要nose --with-coverage来测试主文件夹.py中的tools脚本,并排除tests文件夹(尽管我并不真正关心排除它)

当我运行basic时

nose --with-coverage

我得到所有已安装依赖项和lib(烧瓶、请求等)的覆盖范围

当我跑的时候

nose --with-coverage --cover-package=folder name (or . or ./)

我得到了测试文件夹的覆盖范围。tools/__init__.py文件和app.py但不适用于其余脚本:

> (opentaba-server) D:\username\pathto\opentaba-server>nosetests --with-coverage -- cover-package=./ ... Name                                      

> Stmts   Miss  Cover   Missing
> ----------------------------------------------------------------------- Tests\functional_tests\test_return_json      26      0   100%
> Tests\unit_test\test_createdb                 0      0   100%
> Tests\unit_test\test_scrape                   0      0   100% app     
> 63     15    76%   22, 24, 72-81, 8 8-106, 133 tools\__init__         
> 0      0   100%
> ----------------------------------------------------------------------- TOTAL                                        89     15    83%
> ---------------------------------------------------------------------- Ran 3 tests in 5.182s OK

当我用--cover-inclusive flag运行时。它失败的原因是:

nosetests-scripts.py: error: no such option: --with-coverage

我很高兴能帮上忙


Tags: pytest脚本文件夹apppackageinitwith
3条回答

默认情况下,测试将不包括在覆盖率报告中。您可以使用--cover-tests使它们出现(实际上是确保测试正确执行的一个非常好的主意,并且不会忽略重复的名称测试)

无论如何,nosetests --help是你的朋友。最有可能的是--cover-inclusive标志会终止覆盖率插件,其他选项(对于插件)将不可用。您可以尝试通过pdb启动nose来调试它。

作为替代方案,您可以将覆盖率作为启动nose测试的独立模块运行。

mytests/nose_setup_coverage.cfg文件:

[nosetests]
verbosity=1
detailed-errors=1

with-coverage=1
cover-html=1
cover-html-dir=../../out/cover

#this is the line that fixed my equivalent of your issue
#by "climbing up" from tests/ but skipping python's **site-packages**
cover-package=..   

where=/Users/jluc/kds2/py/tests

添加cover-package=..(在cfg文件中)并从tests目录中执行使我能够覆盖所有python目录,而不是django和其他第三方的内容。

这是我的目录结构(减去一些非Python的东西):

.
├── lib
├── non_app
├── ps_catalog
├── pssecurity
├── pssystem
├── static
├── static_src
├── staticfiles
├── templates
├── tests
└── websec

最后,虽然看起来没有文档记录,但覆盖率(从nose test s运行)将选择并使用当前(test)目录中的.coveragerc文件(您不能通过命令行或nose cfg文件传递它,这是用于coverage插件)。

在该文件中,省略部分允许您更好地控制要排除哪些目录:

omit=/Users/jluc/kds2/env/lib/python2.7/*
     */batch/*
     /Users/jluc/kds2/py/non_app/*
     */migrations/*

在bash中执行测试:

nosetests -c nose_setup_coverage.cfg

p.s.在上面添加--cover-erase,重置覆盖范围

生成的代码也有类似的问题。解决方案是只从报表中排除您的案例中生成的代码或工具代码。

所以我们现在用鼻子测试

nosetests --with-coverage --cover-inclusive --cover-package=$(PACKAGE)

然后,我们手动创建报告,所以

coverage combine
coverage report --omit 'tools/*'

因此,coverage.py将覆盖您的工具包,但它们不会显示在报告中。

相关问题 更多 >