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
13
votes
6 answers

Django Tests: setUpTestData on Postgres throws: "Duplicate key value violates unique constraint"

I am running into a database issue in my unit tests. I think it has something to do with the way I am using TestCase and setUpData. When I try to set up my test data with certain values, the tests throw the following…
Adam Starrh
  • 6,428
  • 8
  • 50
  • 89
13
votes
3 answers

Loading SQL dump before running Django tests

I have a fairly complex Django project which makes it hard/impossible to use fixtures for loading data. What I would like to do is to load a database dump from the production database server after all tables has bene created by the testrunner and…
knutin
  • 5,033
  • 19
  • 26
13
votes
3 answers

Django LiveServerTestCase: User created in in setUpClass method not available in test_method?

I am using Django 1.4's LiveServerTestCase for Selenium testing and am having trouble with the setUpClass class method. As far as I understand MembershipTests.setUpClass is run once before the unit tests are run. I've put code to add a user to the…
Hakan B.
  • 2,319
  • 23
  • 29
13
votes
2 answers

Difference between django-webtest and selenium

I have been reading about testing in django. One thing that was recommended was use of django-webtest for functional testing. I found a decent article here that teaches how to go about functional testing in selenium using python. But people have…
whatf
  • 6,378
  • 14
  • 49
  • 78
12
votes
3 answers

How do I execute an arbitrary script in the context of my Django project?

Sometimes I want to execute a file in the context of my Django project, just as if I were using the shell, but with the convenience of using a text editor. This is mainly to try something out, or quickly prototype some functionality before putting…
LS55321
  • 883
  • 1
  • 12
  • 21
12
votes
1 answer

How to use --failfast in django test command line?

I want to use --failfast so that if my one test fail, remaining all other test gets executed. Do I need to create new test runner or I can use it in command line ?
Pratik Bhatt
  • 871
  • 1
  • 8
  • 21
12
votes
4 answers

Specifying Readonly access for Django.db connection object

I have a series of integration-level tests that are being run as a management command in my Django project. These tests are verifying the integrity of a large amount of weather data ingested from external sources into my database. Because I have…
12
votes
1 answer

Django Testing: Does --keepdb reset changes made during tests?

According to the Django docs regarding tests, the --keepdb flag will preserve the the test database for future runs. https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---keepdb Just to be clear, will any changes made to the…
Adam Starrh
  • 6,428
  • 8
  • 50
  • 89
12
votes
2 answers

Assert that two lists of objects are equal in django testing

Is there a way to check that two lists of objects are equal in django tests. lets say I have some model: class Tag(models.Model): slug = models.SlugField(max_length=50, unique=True) def __unicode__(self): return self.slug and I run…
Ben
  • 6,986
  • 6
  • 44
  • 71
12
votes
1 answer

Django REST Framework - multiple models / APIs?

We use Django to build web-app backend, providing RESTful API for Ember app. So (evolutionally) we started with the following simple structure: project root | |-app1 / models.py .... no views.py | |-app2 / models.py .... no views.py | |-app3 /…
kostik
  • 293
  • 1
  • 4
  • 9
12
votes
5 answers

Can i access the response context of a view tested without the test client?

I have a function which i call from a unittest. From setting some debug traces i know the function worked like a charm and has all the values correctly prepared for return. This is what my testcode looks like (see where my ipdb.set_trace() is…
marue
  • 5,588
  • 7
  • 37
  • 65
12
votes
2 answers

Create a simple password for unittest user using PASSWORD_HASHERS

Until django 1.2.5 i could use the following code to create a user for testing and then log it in: class TestSomeLoginRequiredView(TestCase): urls = 'sonloop.tests.test_urls' def setUp(self): self.user =…
marue
  • 5,588
  • 7
  • 37
  • 65
11
votes
3 answers

How to correctly use assertRaises in Django

I have the following validation function in my model: @classmethod def validate_kind(cls, kind): if kind == 'test': raise ValidationError("Invalid question kind") which I am trying to test as follows: w = Model.objects.get(id=1)…
11
votes
2 answers

How to generate a file upload (test) request with Django REST Framework's APIRequestFactory?

I have developed an API (Python 3.5, Django 1.10, DRF 3.4.2) that uploads a video file to my media path when I request it from my UI. That part is working fine. I try to write a test for this feature but cannot get it to run…
vwrobel
  • 1,706
  • 15
  • 25
11
votes
2 answers

How to test url in django

I would like to test my url in django but I got error message. here are my codes: urls.py url(r'^create/(?P[0-9a-zA-Z-]+)/$', views.create, name='create'), test.py from django.test import TestCase, Client client = Client() def…
James Reid
  • 359
  • 1
  • 6
  • 19