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
203
votes
11 answers

django test app error - Got an error creating the test database: permission denied to create database

When I try to test any app with command (I noticed it when I tried to deploy myproject using fabric, which uses this command): python manage.py test appname I get this error: Creating test database for alias 'default'... Got an error creating the…
Andrius
  • 15,319
  • 31
  • 125
  • 205
152
votes
4 answers

Django Reverse with arguments '()' and keyword arguments '{}' not found

Hi I have an infuriating problem. I have a url pattern like this: # mproject/myapp.urls.py url(r'^project/(?P\d+)/$','user_profile.views.EditProject',name='edit_project'), it works fine in the browser but for testing, when I do this in…
Darwin Tech
  • 16,209
  • 34
  • 102
  • 173
113
votes
3 answers

How should I write tests for Forms in Django?

I'd like to simulate requests to my views in Django when I'm writing tests. This is mainly to test the forms. Here's a snippet of a simple test request: from django.tests import TestCase class MyTests(TestCase): def test_forms(self): …
Mridang Agarwalla
  • 38,521
  • 65
  • 199
  • 353
96
votes
10 answers

Testing email sending in Django

I need to test that my Django application sends e-mails with correct content. I don't want to rely on external systems (like an ad-hoc gmail account), since I'm not testing the actual e-mail service... I would like to, maybe, store the emails…
RadiantHex
  • 22,589
  • 43
  • 141
  • 236
88
votes
6 answers

Django's self.client.login(...) does not work in unit tests

I have created users for my unit tests in two ways: 1) Create a fixture for "auth.user" that looks roughly like this: { "pk": 1, "model": "auth.user", "fields": { "username": "homer", …
thebossman
  • 4,318
  • 10
  • 32
  • 45
81
votes
6 answers

How can I unit test django messages?

In my django application, I'm trying to write a unit test that performs an action and then checks the messages in the response. As far as I can tell, there is no nice way of doing this. I'm using the CookieStorage storage method, and I'd like to…
dvydra
  • 925
  • 1
  • 6
  • 6
73
votes
5 answers

Django : Testing if the page has redirected to the desired url

In my django app, I have an authentication system. So, If I do not log in and try to access some profile's personal info, I get redirected to a login page. Now, I need to write a test case for this. The responses from the browsers I get is : …
Deepankar Bajpeyi
  • 5,036
  • 10
  • 37
  • 60
60
votes
8 answers

Django: is there a way to count SQL queries from an unit test?

I am trying to find out the number of queries executed by a utility function. I have written a unit test for this function and the function is working well. What I would like to do is track the number of SQL queries executed by the function so that…
Manoj Govindan
  • 64,355
  • 21
  • 123
  • 132
59
votes
6 answers

Detect django testing mode

I'm writing a reusable django app and I need to ensure that its models are only sync'ed when the app is in test mode. I've tried to use a custom DjangoTestRunner, but I found no examples of how to do that (the documentation only shows how to define…
Herberth Amaral
  • 3,139
  • 3
  • 30
  • 35
58
votes
2 answers

Django test RequestFactory vs Client

I am trying to decide whether I should use Django's Client or RequestFactory to test my views. I am creating my server using DjangoRESTFramework and it's really simple, so far: class SimpleModelList(generics.ListCreateAPIView): """ Retrieve…
57
votes
5 answers

how to get request object in django unit testing?

I have a function as def getEvents(eid, request): ...... Now I want to write unit test for the above function separately (without calling the view). So how should I call the above in TestCase. Is it possible to create request ?
user1003121
  • 959
  • 3
  • 8
  • 14
53
votes
6 answers

Using Basic HTTP access authentication in Django testing framework

For some of my Django views I've created a decorator that performs Basic HTTP access authentication. However, while writing test cases in Django, it took me a while to work out how to authenticate to the view. Here's how I did it. I hope somebody…
Humphrey
  • 3,560
  • 2
  • 24
  • 26
47
votes
6 answers

How to launch tests for django reusable app?

Can I launch tests for my reusable Django app without incorporating this app into a project? My app uses some models, so it is necessary to provide (TEST_)DATABASE_* settings. Where should I store them and how should I launch tests? For a Django…
dzida
  • 8,092
  • 2
  • 33
  • 55
41
votes
2 answers

Writing test cases for django models

Half way through my current project, after suffering the pain of spending uncountable minutes on debugging, I have decided to adopt TDD. To start, I am planning to write a set of unit tests for each existing models. But for models that only have…
tamakisquare
  • 15,251
  • 24
  • 79
  • 125
40
votes
1 answer

Is APITest with Query params different then just normal url?

I'm writing some unit tests against an API that either returns all the books, or only returns the books of the given genre in the query params. This seems to be working when I hit it in my local dev server. However, it doesn't even go into the else…
user133688
  • 5,732
  • 3
  • 16
  • 30
1
2 3
68 69