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.

1220 questions
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
  • 24,602
  • 6
  • 66
  • 90
28
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,363
  • 1
  • 11
  • 22
26
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
  • 18,449
  • 38
  • 112
  • 187
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
  • 48,559
  • 18
  • 128
  • 201
25
votes
5 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
  • 519
  • 1
  • 7
  • 13
25
votes
2 answers

factory boy: define field that depends on other field

How to define a field that depends on other field using factory-boy? For instance, I'd like to define an email that depends on the first name and last name of an User. I tried using the post_generation decorator. However, my system requires the…
jsmedmar
  • 1,025
  • 10
  • 21
25
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,739
  • 5
  • 18
  • 25
24
votes
2 answers

Difference between TestCase and TransactionTestCase classes in django test

Please explain the difference between the TestCase class and TransactionTestCase class. I have read the documentation but it's only saying that TestCase runs tests in a database transaction and uses rollback to "undo" the test in the database, and…
vishnu m c
  • 841
  • 1
  • 7
  • 21
24
votes
10 answers

django how to see sql query when running tests?

One of my django application unit test fails with DatabaseError: ORA-00942: table or view does not exist I would like to see actual SQL query that caused this error. Do you know how to achieve that?
mnowotka
  • 16,430
  • 18
  • 88
  • 134
24
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
  • 16,273
  • 32
  • 108
  • 163
23
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
  • 611
  • 7
  • 17
22
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
  • 12,515
  • 14
  • 51
  • 71
21
votes
5 answers

ImportError: Failed to import test module:

im having an issue with a tutorial im following. we have gotten to the point of testing and i continue to get an error when running python manage.py test Here is my error: (restapi)…
Cflux
  • 1,423
  • 3
  • 19
  • 39
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
  • 4,029
  • 6
  • 33
  • 47
20
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
  • 8,307
  • 9
  • 51
  • 75
1 2
3
81 82