Django测试覆盖率与代码覆盖率 [英] Django test coverage vs code coverage

查看:200
本文介绍了Django测试覆盖率与代码覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功安装并配置了 django-nose 覆盖

I've successfully installed and configured django-nose with coverage

问题是,如果我只是为 ./ manage.py shell 运行覆盖,并退出该shell - 它显示我37%代码覆盖。我完全明白执行的代码并不意味着测试代码。我唯一的问题是 - 现在是什么?

Problem is that if I just run coverage for ./manage.py shell and exit out of that shell - it shows me 37% code coverage. I fully understand that executed code doesn't mean tested code. My only question is -- what now?

我在设想的是可以在执行任何测试之前导入所有的python模块和安定,直接与 coverage 进行沟通,说好的,开始计数到这里的代码。

What I'm envisioning is being able to import all the python modules and "settle down" before executing any tests, and directly communicating with coverage saying "Ok, start counting reached code here."

理想情况下,这将通过 nose 在执行每个测试套件之前,基本上重置了触摸的代码行。

Ideally this would be done by nose essentially resetting the "touched" lines of code right before executing each test suite.

我不知道开始寻找/发展。我已经在线搜索,没有找到任何有成果的东西。任何帮助/指南将不胜感激。

I don't know where to start looking/developing. I've searched online and haven't found anything fruitful. Any help/guidelines would be greatly appreciated.

PS

我尝试执行如下操作:

DJANGO_SETTINGS_MODULE=app.settings_dev coverage run app/tests/gme_test.py

它的工作(显示1%的覆盖率),但我无法弄清楚如何为整个应用程序这样做

And it worked (showed 1% coverage) but I can't figure out how to do this for the entire app

编辑:这是我的覆盖配置:

Edit: Here's my coverage config:

[run]
source = .
branch = False
timid = True
[report]
show_missing = False
include = *.py
omit =
    tests.py
    *_test.py
    *_tests.py
    */site-packages/*
    */migrations/*
[html]
title = Code Coverage
directory = local_coverage_report


推荐答案

django-nose你有两个选择如何运行覆盖。第一个已经由DaveB指出:

since you use django-nose you have two options on how to run coverage. The first was already pointed out by DaveB:

coverage run ./manage.py test myapp

以上实际运行的覆盖范围,然后监视由test命令执行的所有代码。

The above actually runs coverage which then monitors all code executed by the test command.

但是,还有一个鼻子覆盖插件包含在django-nose包中( http://nose.readthedocs.org/en/latest/plugins/cover.html )。你可以这样使用:

But then, there is also a nose coverage plugin included by default in the django-nose package (http://nose.readthedocs.org/en/latest/plugins/cover.html). You can use it like this:

./manage.py test myapp --with-coverage

(还有一些额外的选项,包括哪些模块应该涵盖,是否包括html报告等)在上述链接中,您还可以输入 ./ manage.py test --help 获取一些快速信息。

(There are also some additional options like which modules should be covered, whether to include an html report or not etc . These are all documented in the above link - you can also type ./manage.py test --help for some quick info).

运行鼻子覆盖插件将导致覆盖运行后,执行django引导代码,因此相应的代码将不会被报告覆盖。

Running the nose coverage plugin will result in coverage running after the django bootstrapping code is executed and therefore the corresponding code will not be reported as covered.

在运行原始方式时报告的大多数代码都是import语句,类定义,类成员等。由于python在导入时间内对它们进行了评估,因此覆盖自然会被标记为覆盖。然而,运行鼻插件将不会报告自从在加载django环境后测试运行器启动的引导代码。当然,这样做的另一个副作用是你永远不能实现100%的覆盖(...或关闭:)),因为您的全球范围声明将永远不会被覆盖。

Most of the code you see reported as covered when running coverage the original way, are import statements, class definitions, class members etc. As python evaluates them during import time, coverage will naturally mark them as covered. However, running the nose plugin will not report bootstrapping code as covered since the test runner starts after the django environment is loaded. Of course, a side effect of this is you can never achieve 100% coverage (...or close :)) as your global scope statements will never get covered.

在覆盖选项之后,我现在已经结束了这样的覆盖:

After switching back and forth and playing around with coverage options, I now have ended up using coverage like this:

coverage run --source=myapp,anotherapp ---omit=*/migrations/* ./manage.py test

所以a)覆盖报告导入语句,类成员定义等等(实际上是事实 - 这个代码被成功导入和解释)和b)它将只覆盖我的代码而不是django代码,或任何其他我使用的第三方应用程序;覆盖率将反映我的项目覆盖率。希望这有帮助!

so that a) coverage will report import statements, class member definitions etc as covered (which is actually the truth - this code was successfully imported and interpreted) and b) it will only cover my code and not django code, or any other third-party app I use; the coverage percentage will reflect how well my project is covered. Hope this helps!

这篇关于Django测试覆盖率与代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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