Cache middleware causing unit tests to fail

92 views
Skip to first unread message

Norman Harman

unread,
Aug 16, 2007, 7:05:55 PM8/16/07
to django...@googlegroups.com
Hi,

request.template is None instead of what it should be because the response is from the
cache middleware.

Which is fine. My question is there a good way to disable the cache middleware during
tests?

"good" being atleast the following. is automatic, isn't based of value of DEBUG, doesn't
require changing django distro.

Is there a special settings file or other file imported during tests only?

thanks,
njharman

Malcolm Tredinnick

unread,
Aug 17, 2007, 1:18:37 AM8/17/07
to django...@googlegroups.com

The standard (and recommended) approach if you need slightly different
settings during testing is to create a new settings file just for tests.
It will look something like this:

from settings import *
CACHE_BACKEND = 'simple://'

where "settings" is your normal settings file. This way you only
override the settings you need to. Then you run your tests with

manage.py --settings=test_settings test

Note that if you are looking for a way to entirely ignore the cache
middleware, you probably need to remove it from the middleware list.
Since you test_settings.py file can contain arbitrary Python code in it,
you can do that by examing your existing MIDDLEWARE_CLASSES list,
removing the offending entry and reassigning the result to
MIDDLEWARE_CLASSES.

Regards,
Malcolm

--
Depression is merely anger without enthusiasm.
http://www.pointy-stick.com/blog/

Eratothene

unread,
Aug 19, 2007, 1:29:21 AM8/19/07
to Django users
I think you better not disable cache middleware in tests. If tests
fail, so will be in production. I had similar problem, I thought it
was something wrong with tests, but really it was problem in the code.
My site was running well on django built-in server, but not on apache
mod_python.

The problem was in incorrect order of middlewares. Cache middleware
must be after session middleware. Personally I think it is bug django
that causes such behavior. Please post your traceback of the failed
tests.

'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.cache.CacheMiddleware',

Placid Publishing, LLC

unread,
Aug 19, 2007, 1:43:13 AM8/19/07
to django...@googlegroups.com
Sadly there is not much to trace back, the dev server runs fine with
memcache, but mod_python/apache doesn't. Mod_python/apache work with the
file system cache as does the dev server, with memcache as the cache
storage mod_python/apache will just use up almost all cpu and ram, and
the load never goes down (unlike a restart without cache or with file
system cache). The site isn't busy, and sockstat shows httpd processes
connecting to memcache then disconnecting frequently. I've checked thru
the code and it looks good, cmemcache shouldn't be giving me any
problems. Middlewear order also looks fine.

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.middleware.cache.CacheMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)

Sasha Weberov

unread,
Aug 19, 2007, 1:44:48 AM8/19/07
to Django users

On Aug 19, 12:43 am, "Placid Publishing, LLC"

> >> njharman- Hide quoted text -
>
> - Show quoted text -


DOH, my bad, I thought this was directed towards me from another post.
SORRY :-|

Norman Harman

unread,
Aug 20, 2007, 12:38:06 AM8/20/07
to django...@googlegroups.com
Eratothene wrote:
> I think you better not disable cache middleware in tests. If tests
> fail, so will be in production. I had similar problem, I thought it
> was something wrong with tests, but really it was problem in the code.
> My site was running well on django built-in server, but not on apache
> mod_python.

My site was working fine. My unittests was what were in error.

Yeah there are going to be differences between production and devel. But, personally I
believe unittests should test one thing. Not many things at once such as cache/view/db
(as an aside I'm sort of sad django doesn't have a mock db for view unittests)

So, I think you should test your cache framework and how your app uses it, but, those
tests should not be part of the view unittests.

I was testing that a specific template was being used to render page. With caching the
correct behavior is to not use any template, not to render at all, but rather to pump
contents of cache down the wire.

In my test_settings.py file I have:
# disable cache for testing
MIDDLEWARE_CLASSES.remove('django.middleware.cache.CacheMiddleware')
CACHE_BACKEND = "dummy:///"

Then run
python manage.py --settings=test_settings test
works beautifully.

Reply all
Reply to author
Forward
0 new messages