Questions tagged [django-tests]

Use for questions about testing applications based upon the Python web framework Django, or the features provided in the module itself (django.test)

docs: https://docs.djangoproject.com/en/2.1/topics/testing/overview/

450 questions
40
votes
4 answers

When I run test cases I get this error: psycopg2.OperationalError: cursor "_django_curs_140351416325888_23" does not exist

I'm trying to run test cases, but I get below error. Run command : python manage.py test Type 'yes' if you would like to try deleting the test database 'test_project_management_db', or 'no' to cancel: yes Destroying old test database for alias…
Ankit Kumar Rathod
  • 503
  • 1
  • 4
  • 7
25
votes
3 answers

django update_or_create gets "duplicate key value violates unique constraint "

Maybe I misunderstand the purpose of Django's update_or_create Model method. Here is my Model: from django.db import models import datetime from vc.models import Cluster class Vmt(models.Model): added =…
Red Cricket
  • 9,762
  • 21
  • 81
  • 166
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
20
votes
4 answers

Django Rest Framework API Client Custom Header

I am trying to use bulk update from drf-extensions. To make it work, there is a safeguard requiring the header "X-BULK-OPERATION": 'true'. I can get the application working using curl or my angular app, but in my tests I am trying to use…
Murphy4
  • 1,499
  • 2
  • 15
  • 22
19
votes
6 answers

What is actually assertEquals in Python?

I have the following test.py file in django. can you please explain this code? from contacts.models import Contact ... class ContactTests(TestCase): """Contact model tests.""" def test_str(self): contact =…
Rockhound
  • 407
  • 1
  • 3
  • 9
16
votes
2 answers

How to avoid creating test database for testing in django?

I have written some test cases for my project when I run these test cases, it creates test database for alias 'default' every time, after giving message then destroy database. I am concern only with message, So How to avoid creating test database,…
Neelabh Singh
  • 2,600
  • 12
  • 51
  • 88
14
votes
3 answers

How to use pytest fixtures with django TestCase

How can I use a pytest fixture within a TestCase method? Several answers to similar questions seem to imply that my example should work: import pytest from django.test import TestCase from myapp.models import Category pytestmark =…
Lord Elrond
  • 13,430
  • 7
  • 40
  • 80
12
votes
3 answers

how to test a model that has a foreign key in django?

I'm using python 3.5 and Django 1.10 and trying to test my app in tests.py, but an error appeared, it said: ValueError: Cannot assign "1": "NewsLetter.UserID" must be a "User" instance. so how to test a fk value here? here is the code: class…
mg22
  • 245
  • 2
  • 6
  • 13
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
2 answers

Start django shell with a temporary database

I want to fire up the django shell with a temporary database (like what's done when doing django tests) Is there any command like: python manage.py testshell where I can create a bunch of bogus models without polluting my database?
Ben
  • 6,986
  • 6
  • 44
  • 71
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)…
10
votes
3 answers

Django testing and ContentType generic relatations fixtures

How do I use GenericRelations in Django unit testing? I have read and tried countless suggestions on the internets to no avail. This one was promising Problems with contenttypes when loading a fixture in Django but "reset" command is no longer…
user3138929
  • 369
  • 5
  • 18
8
votes
2 answers

Django abstract model + DB migrations: tests throw "cannot ALTER TABLE because it has pending trigger events"

I want to write an abstract model mixin, that I can use to make OneToOne - relations to the user model. Here is my code: from django.conf import settings from django.db import models class Userable(models.Model): user = models.OneToOneField( …
J. Hesters
  • 13,117
  • 31
  • 133
  • 249
8
votes
2 answers

Add Token to headers request in TEST mode DRF

how is possible to add 'Authorization': 'Token' to TEST request in Django/DRF? If i use simple requests.get(url, headers={'Authorization': 'Token'} all work perfect but how to do such request in TestCase?
Beliaf
  • 577
  • 2
  • 8
  • 25
1
2 3
29 30