2 years ago

#257500

test-img

sunw

Why does accessing Django cache during unit tests (manage.py test) produces datetime.datetime objects instead of expected values?

I have a Django app, and I'm trying to create unit tests for functions/methods that work with the cache. My tests are run using python manage.py test using the same Docker container that I use for running the app locally. The functions/methods I'm testing contain lines that are similar to the following:

from django.core.cache import cache
...
def some_function():
    ...
    cache.set('test', ('hello', 'world'))
    retrieved = cache.get('test')
    ...

I expect retrieved to be the value that I retrieved from the cache (in this case, ('hello', 'world')); however, retrieved is somehow always a datetime.datetime object. This happens for various different cache backends, including django.core.cache.backends.locmem.LocMemCache and django_redis.cache.RedisCache. How can I correct this cache behavior for tests?

django

caching

django-testing

django-tests

django-redis

0 Answers

Your Answer

Accepted video resources