17

I have moved my Django app from my development machine (OS X, Python 2.6.5, Django 1.2.3) to a staging server (Ubuntu VM, Python 2.6.6, Django 1.2.3).

If I now run my test suite on the staging server, two tests fail when using the Django TestClient because response.context is None (but response.content is correct).

For example:

self.assertEquals(self.session.pk, response.context['db_session'].pk)

These test cases pass on the development machine.

Has anybody encountered similar problems?

epoch
  • 1,307
  • 2
  • 15
  • 23
  • So the above assertion throws an error claiming that response.context is None? Can we see a stacktrace? – eternicode Nov 10 '10 at 21:29
  • Here is another discussion of likely the same issue: [Django unit test response context is None](http://stackoverflow.com/questions/27136048/django-unit-test-response-context-is-none/36703491#36703491) – woodz Apr 18 '16 at 20:55
  • For me this error happened when I enabled memcache caching on my development instance. After changing the cache configuration back to the DummyCache the error did vanish. – tobltobs May 26 '16 at 15:30

2 Answers2

11

You need to add the test setup statement.

import django
django.test.utils.setup_test_environment() 

Find more details by following my link: http://jazstudios.blogspot.com/2011/01/django-testing-views.html

woodz
  • 417
  • 3
  • 9
Joe J
  • 8,079
  • 8
  • 57
  • 84
0

From Django documentation:

Although * your code * [+] would work in the Python interactive interpreter, some of the test client's functionality, notably the template-related functionality, is only available while tests are running. The reason for this is that Django's test runner performs a bit of black magic in order to determine which template was loaded by a given view. This black magic (essentially a patching of Django's template system in memory) only happens during test running.

So if you run it in a test run, it should work.

You can see this question

fedotsoldier
  • 184
  • 14