Questions tagged [django-testing]

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. This tag is about writing and running tests for your Django apps.

Django

Testing overview

Testing a Web application is a complex task, because a Web application is made of several layers of logic – from HTTP-level request handling, to form validation and processing, to template rendering. With Django’s test-execution framework and assorted utilities, you can simulate requests, insert test data, inspect your application’s output and generally verify your code is doing what it should be doing.

1023 questions
38
votes
3 answers

Django tests - patch object in all tests

I need to create some kind of MockMixin for my tests. It should include mocks for everything that calls external sources. For example, each time I save model in admin panel I call some remote URLs. It would be good, to have that mocked and use like…
tunarob
  • 2,404
  • 4
  • 25
  • 46
38
votes
5 answers

How to test custom template tags in Django?

I'm adding a set of template tags to a Django application and I'm not sure how to test them. I've used them in my templates and they seem to be working but I was looking for something more formal. The main logic is done in the models/model…
Mark Lavin
  • 22,998
  • 4
  • 71
  • 67
38
votes
2 answers

What are the best practices for testing "different layers" in Django?

I'm NOT new to testing, but got really confused with the mess of recommendations for testing different layers in Django. Some recommend (and they are right) to avoid Doctests in the model as they are not maintainable... Others say don't use…
Soask
  • 671
  • 11
  • 21
36
votes
9 answers

Mocking a Django Queryset in order to test a function that takes a queryset

I have a utility function in my Django project, it takes a queryset, gets some data from it and returns a result. I'd like to write some tests for this function. Is there anyway to 'mock' a QuerySet? I'd like to create an object that doesn't touch…
Rory
  • 48,706
  • 67
  • 174
  • 234
35
votes
8 answers

Want to disable signals in Django testing

So I have various signals and handlers which are sent across apps. However, when I perform tests / go into 'testing mode', I want these handlers to be disabled. Is there a Django-specific way of disabling signals/handlers when in testing mode? I can…
user2564502
  • 639
  • 2
  • 7
  • 15
35
votes
6 answers

How do I test Django QuerySets are equal?

I am trying to test my Django views. This view passes a QuerySet to the template: def merchant_home(request, slug): merchant = Merchant.objects.get(slug=slug) product_list = merchant.products.all() return…
jz999
  • 529
  • 1
  • 4
  • 13
32
votes
3 answers

Running django tests with sqlite

I use Postgres for production and development, but I'd like to use sqlite to run some tests. I don't see an easy way to configure one engine for tests and another for dev / production. Am I missing something?
jMyles
  • 10,434
  • 6
  • 39
  • 54
31
votes
1 answer

Specify Django Test Database names in settings.py

I'm specifying the databases using a python object: DATABASES = { 'default':{ 'ENGINE':'mysql', 'NAME':'testsqldb', 'USER':'', 'PASSWORD':'', }, 'dynamic_data':{ 'ENGINE': 'sqlite3', 'NAME':'', …
Michael Merchant
  • 1,462
  • 2
  • 17
  • 27
31
votes
5 answers

How do I modify the session in the Django test framework

My site allows individuals to contribute content in the absence of being logged in by creating a User based on the current session_key I would like to setup a test for my view, but it seems that it is not possible to modify the request.session: I'd…
Riley
  • 1,008
  • 1
  • 11
  • 9
29
votes
1 answer

Testing Django apps that use South migrations

I'm trying to create some Functional tests for a Django app that uses South migrations. Eventually, I will also be creating Twill tests. When I try to run the existing tests, the test database is not created successfully because of a problem with…
BryanWheelock
  • 11,386
  • 18
  • 60
  • 101
29
votes
2 answers

Writing good tests for Django applications

I've never written any tests in my life, but I'd like to start writing tests for my Django projects. I've read some articles about tests and decided to try to write some tests for an extremely simple Django app or a start. The app has two views (a…
Ludwik Trammer
  • 21,961
  • 6
  • 61
  • 86
28
votes
5 answers

Django test FileField using test fixtures

I'm trying to build tests for some models that have a FileField. The model looks like this: class SolutionFile(models.Model): ''' A file from a solution. ''' solution = models.ForeignKey(Solution) file =…
sttwister
  • 2,049
  • 1
  • 18
  • 22
25
votes
2 answers

IntegrityError when loading fixture during django testing

I'm loading a fixture created with dumpdata, and getting the following exception: Problem installing fixture 'db_dump.json': Traceback (most recent call last): File "/usr/lib/python2.6/site-packages/django/core/management/commands/loaddata.py",…
Marcin
  • 44,601
  • 17
  • 110
  • 191
25
votes
2 answers

How to use Django's assertJSONEqual to verify response of view returning JsonResponse

I'm using Python 3.4 and Django 1.7. I have a view returning JsonResponse. def add_item_to_collection(request): #(...) return JsonResponse({'status':'success'}) I want to verify if that view returns correct response using unit test: class…
Mariusz Jamro
  • 27,234
  • 22
  • 104
  • 144
24
votes
1 answer

How do I setup a unit test user for django app? The unit test can't login

I've enabled login authentication for my django app. Unit tests for views are not working because they get stuck at the login page. Setting a breakpoint immediately after the view's response is returned and using print response.content results…
dolphus333
  • 1,023
  • 1
  • 10
  • 18
1
2
3
68 69