alo*_*ser 27 python nose nosetests
我的项目文件夹(是的 - 我知道这是最好的做法)是这样的:
.
??? app.py
??? otherscript.py
??? tests/
??? tools/
??? __init__.py
??? toolfile.py
Run Code Online (Sandbox Code Playgroud)
我需要nose --with-coverage测试.py主文件夹,tools文件夹中的脚本并排除tests文件夹(虽然我真的不在乎排除它)
当我运行基本
nose --with-coverage
Run Code Online (Sandbox Code Playgroud)
我得到了所有安装的依赖项和库(烧瓶,请求等)的报道
我跑的时候
nose --with-coverage --cover-package=folder name (or . or ./)
Run Code Online (Sandbox Code Playgroud)
我得到了测试文件夹的覆盖范围.该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
Run Code Online (Sandbox Code Playgroud)
我跑的时候--cover-inclusive flag.它只是失败了:
nosetests-scripts.py: error: no such option: --with-coverage
Run Code Online (Sandbox Code Playgroud)
我很乐意为此提供任何帮助
Geo*_*org 13
生成的代码我遇到了类似的问题.解决方案是仅从报告中排除生成的代码或工具代码.
所以我们现在使用nosetests来运行我们的测试
nosetests --with-coverage --cover-inclusive --cover-package=$(PACKAGE)
Run Code Online (Sandbox Code Playgroud)
然后,我们手动创建报告,所以
coverage combine
coverage report --omit 'tools/*'
Run Code Online (Sandbox Code Playgroud)
因此,coverage.py将涵盖您的工具包,但它们不会显示在报告中.
我的tests/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
Run Code Online (Sandbox Code Playgroud)
添加cover-package=..(在cfg文件中)并在tests目录中执行让我覆盖了我所有的python目录,但不包括django和其他第三方的东西.
这是我的目录结构(减去一些非Python的东西):
.
??? lib
??? non_app
??? ps_catalog
??? pssecurity
??? pssystem
??? static
??? static_src
??? staticfiles
??? templates
??? tests
??? websec
Run Code Online (Sandbox Code Playgroud)
最后,虽然它似乎没有记录,但是从nosetests运行的覆盖范围将在当前(测试)目录中选择并使用.coveragerc文件(你不能通过命令行或鼻子cfg文件传递它,这是为了所述盖插件).
在该文件中,省略部分允许您更好地控制要排除的目录:
omit=/Users/jluc/kds2/env/lib/python2.7/*
*/batch/*
/Users/jluc/kds2/py/non_app/*
*/migrations/*
Run Code Online (Sandbox Code Playgroud)
在bash中执行测试:
nosetests -c nose_setup_coverage.cfg
ps添加--cover-erase到上面,重置覆盖范围
默认情况下,测试不会包含在覆盖率报告中。您可以让它们显示出来(实际上是一个非常好的主意,可以确保您的测试正确执行,并且不会忽略重复的名称测试)--cover-tests
无论如何,nosetests --help是你的朋友。最有可能的--cover-inclusive标志会杀死覆盖率插件,并且其他选项(对于插件)变得不可用。您可以尝试通过启动nose来调试它pdb。
作为替代方案,您可以将覆盖率作为启动鼻子测试的独立模块来运行。