Questions tagged [django-unittest]

django-unittest tag refers to writing unit tests in Django, tests that are expressed as methods on a Python class that subclasses unittest.TestCase or Django's customized TestCase.

django-unittest tag refers to writing unit tests in Django, tests that are expressed as methods on a Python class that subclasses unittest.TestCase or Django’s customized TestCase.

329 questions
2
votes
2 answers

How do I test a Django site that uses UDFs in the database?

I have a django project that uses a postgres db with a number of UDFs. The UDFs are written in plpythonu. Since plpyhtonu is an untrusted language, only database super users can use it to to create UDFs. This poses a serious problem in that I…
Kevin
  • 3,391
  • 5
  • 30
  • 40
2
votes
1 answer

How do I freeze time and assert on a timestamp in a template?

p1/urls.py: from django.urls import path from . import views urlpatterns = [ path('t1/', views.t1), ] p1/views.py: from django.shortcuts import render def t1(request): return render(request, 'p1/t1.html') p1/templates/p1/t1.html:
x-yuri
  • 16,722
  • 15
  • 114
  • 161
2
votes
1 answer

Django overriding settings.py OBJECT in unittest not working

There is Django app that uses Django Rest Framework. The settings file contains an object with the settings: settings.py REST_FRAMEWORK = { ... 'DEFAULT_THROTTLE_RATES': { 'burst': '30/second', }, ... } The unittest is…
0leg
  • 13,464
  • 16
  • 70
  • 94
2
votes
1 answer

Mocking form in class based view not using the MagicMock

I've been fighting with mocking a form class to replace an instance of it in a class-based view. But it looks like that because the form is in a class attribute, it happens before I replace the form class with my mock. Case in…
2
votes
2 answers

Running the django tests using fabric

I have a fabric script that attempts to run the tests for an application that I wrote. It is already installed using setup.py and I am able to import it. I am following the script I found here. My problem is that django-admin doesn't find my app: …
the_drow
  • 18,571
  • 25
  • 126
  • 193
2
votes
0 answers

Missing default model permissions during unit testing in Django app

There are default model permissions for view/add/update/delete in database when applying migrations(it's working with my main DB): sadmonad=# select count(*) from auth_permission; count ------- 32 (1 row) But in django.test it is not the case: it…
ikigai
  • 19
  • 3
2
votes
1 answer

Python Django Test for One of Two Exceptions

I am trying to test that a certain instance of a model will raise one of two exceptions, but I can't figure out how to get it to work. Here is what I have: Model class AvailablePermissions(models.Model): # Main Checkbox. category =…
2
votes
1 answer

Logger results not appearing in unittest output with Django

I'm having difficulty getting the desired behavior at the intersection of the django test runner, i.e. from django.test import TestCase and the logging solution. my logger is set up and works in other areas with something like this import…
Ben
  • 4,980
  • 3
  • 43
  • 84
2
votes
1 answer

form.is_valid() failing in Django unit test

I'm running a unit test for a Django form, and form.is_valid() keeps returning False and I cannot find the error. Here's the code for forms.py: class CustomClearableFileInput(forms.ClearableFileInput): template_name =…
2
votes
0 answers

Mock Django ModelChoiceField's database call

I'd like to test a form without the tests hitting the database. The form references a Model using a ModelChoiceField which in turn runs a query during form.is_valid() that tries to fetch the referenced object from the database. This happens in…
jnns
  • 5,148
  • 4
  • 47
  • 74
2
votes
0 answers

Saving Django model throws OperationalError: cannot start a transaction within a transaction

I'm seeing a weird error in a Django unittest where, on rare occasions, attempting to save a model instance will throw the exception: OperationalError: cannot start a transaction within a transaction What would cause this? The really odd thing is…
Cerin
  • 60,957
  • 96
  • 316
  • 522
2
votes
0 answers

Django unittest with not raising exception as expected with assertRaises()

I'm writing a unittest class to ensure a method tests for a success, and then tests for an Exception. I'm passing a response that should trigger the exception, but in the testing method it does not get raised. Of note, I can manually make the…
user1026996
2
votes
1 answer

How to test get request in Django with a data?

I am now writing tests for my web application in Django. I have an URL 127.0.0.1/home/device/(?P[^/]+). I was testing an invalid URL path. Here item is a device name in the database. Consider an invalid device and while testing I have given…
vishnu m c
  • 841
  • 1
  • 7
  • 21
2
votes
0 answers

Running Django Unit Tests on Bamboo with Postgresql

I am using Django 1.9.6. I have, for example, the following model in my models.py file: class Question(BaseModel): question_text = models.CharField(max_length=500, unique=True) class Meta: verbose_name = 'Question' …
wogsland
  • 9,106
  • 19
  • 57
  • 93
2
votes
0 answers

How to use django-tls in Django unit tests

I'm trying to write some unit tests on a Django (I'm using Django==1.9.12) application that uses django-tls, but I always get this exception: RuntimeError: no object bound to request. My code looks like this: settings.py: As in django-tls doc I…
Vladir Parrado Cruz
  • 2,301
  • 21
  • 27