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
23
votes
5 answers

Django manage.py : Is it possible to pass command line argument (for unit testing)

Is it possible to pass command line arguments to Django's manage.py script, specifically for unit tests? i.e. if I do something like manage.py test myapp -a do_this Can I receive the value do_this in the setUp function of unit test? P.S. @Martin…
user4150760
  • 2,229
  • 4
  • 13
  • 22
22
votes
3 answers

Django test to use existing database

I'm having a hard time customizing the test database setup behavior. I would like to achieve the following: The test suites need to use an existing database The test suite shouldn't erase or recreate the database instead load the data from a mysql…
endre
  • 1,303
  • 10
  • 22
21
votes
3 answers

Problems using User model in django unit tests

I have the following django test case that is giving me errors: class MyTesting(unittest.TestCase): def setUp(self): self.u1 = User.objects.create(username='user1') self.up1 = UserProfile.objects.create(user=self.u1) def…
theycallmemorty
  • 11,427
  • 12
  • 47
  • 68
21
votes
2 answers

How to use pdb.set_trace() in a Django unittest?

I want to debug a Django TestCase just like I would any other Python code: Simply call pdb.set_trace() and then drop into an interactive session. When I do that, I don't see anything, as the tests are run in a different process. I'm using…
Brian Dant
  • 3,579
  • 5
  • 26
  • 43
20
votes
1 answer

difference between django.test.TestCase vs unittest vs django.utils.unittest.TestCase

I am still using Django 1.2.1, and I think with the newer Django we don't import unittest and then do unittest.TestCase. Illustration import unittest class TestThis(unittest.TestCase): from django.utils.unittest import TestCase class…
CppLearner
  • 14,204
  • 29
  • 102
  • 157
19
votes
5 answers

Django testing: Test the initial value of a form field

I have a view that should be setting an initial value for a form field based on a GET value. I want to test this. I'm currently using Django's test client but I am open to looking at other tools. Edit Sorry, I did not mention that I am well aware of…
Belmin Fernandez
  • 7,277
  • 8
  • 46
  • 75
19
votes
11 answers

Choose test database?

I'm trying to run ./manage.py test But it tells me Got an error creating the test database: permission denied to create database Obviously it doesn't have permission to create the database, but I'm on a shared server, so there's not much I can do…
mpen
  • 237,624
  • 230
  • 766
  • 1,119
19
votes
1 answer

Django testing stored session data in tests

I have a view as such: def ProjectInfo(request): if request.method == 'POST': form = ProjectInfoForm(request.POST) if form.is_valid(): # if form is valid, iterate cleaned form data # and save data to…
Darwin Tech
  • 16,209
  • 34
  • 102
  • 173
18
votes
4 answers

Factory Boy random choice for a field with field option "choices"

When a field in a Django model has the option choices, see Django choices field option, it utilises an iterable containing iterables of 2 items to define which values are allowed. For example: Models class IceCreamProduct(models.Model): …
Robin
  • 390
  • 1
  • 5
  • 13
18
votes
7 answers

Selenium: Element not clickable ... Other Element Would Receive Click

When running Selenium tests on my Django project, I've started to get the error: selenium.common.exceptions.WebDriverException: Message: Element is not clickable at point (61, 24.300003051757812). Other element would receive the click:
jejy2343
  • 399
  • 1
  • 5
  • 18
18
votes
4 answers

Django Testing - Hard code URLs or Not

This is a best-practices question. When writing tests in Django, is it better to hard code urls in your tests.py, or to use the dispatch's reverse() function to retrieve the correct url? Using hard-coded urls for testing only feels like the right…
T. Stone
  • 18,208
  • 15
  • 67
  • 95
18
votes
2 answers

Django test client gets 301 redirection when accessing url

I am writing unittests for django views. I have observed that one of my views returns redirection code 301, which is not expected. Here is my views.py mentioned earlier. def index(request): return render(request, 'index.html', …
Michał Klich
  • 561
  • 6
  • 16
17
votes
2 answers

Django testing tips

In the spirit of this question, I would like to know if anyone has any tips on creating a useful and "complete" test suite (can a test suite ever be "complete"?) for a Django webapp. My situation: I've knocked out a prototype and am now working on…
Belmin Fernandez
  • 7,277
  • 8
  • 46
  • 75
17
votes
2 answers

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…
epoch
  • 1,307
  • 2
  • 15
  • 23
16
votes
2 answers

Setting HTTP_REFERER header in Django test

I'm working on a Django web application which (amongst other things) needs to handle transaction status info sent using a POST request. In addition to the HTTP security supported by the payment gateway, my view checks request.META['HTTP_REFERER']…
supervacuo
  • 8,420
  • 2
  • 37
  • 59
1 2
3
68 69