Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django test client response context None

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?

like image 521
epoch Avatar asked Nov 10 '10 10:11

epoch


People also ask

What is client in Django test?

The test client is a Python class that acts as a dummy web browser, allowing you to test your views and interact with your Django-powered application programmatically.

How do I test my Django app?

The preferred way to write tests in Django is using the unittest module built-in to the Python standard library. This is covered in detail in the Writing and running tests document. You can also use any other Python test framework; Django provides an API and tools for that kind of integration.

What is self client in Python?

self. client , is the built-in Django test client. This isn't a real browser, and doesn't even make real requests. It just constructs a Django HttpRequest object and passes it through the request/response process - middleware, URL resolver, view, template - and returns whatever Django produces.


1 Answers

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

like image 193
Joe J Avatar answered Oct 08 '22 22:10

Joe J