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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With