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
0 answers

How to run raw SQL inside a Django unittest

How do you run a raw SQL query inside a Django unittest? I find if I use a cursor, it seems to break the unittest's transaction so that any changes to the Sqlite database aren't reverted at the end of the unittest. My unittest looks like: class…
Cerin
  • 60,957
  • 96
  • 316
  • 522
2
votes
2 answers

Django mock unit test form filefield

I have a form that I want to unittest: app/form.py class MyForm(forms.Form): file = forms.FileField() app/test.py class MyFormTest(TestCase): def test_my_form(self): file_mock = MagicMock(spec=File) form =…
Al-Alamin
  • 1,438
  • 2
  • 15
  • 34
2
votes
1 answer

Unit test checking if database queries are correct – what can be mocked?

Let's say I have an Article model, like this: from django.db import models class Article(models.Model): author = models.CharField(max_length=100) title = models.CharField(max_length=200) body = models.TextField() This is naively simple…
2
votes
0 answers

How to disable CSRF for Django live server testing?

How do you disable CSRF for live server tests in Django when using Selenium? Any page the browser tries to visit with Selenium throws a CSRF failure. I've tried modifying my settings to remove django.middleware.csrf.CsrfViewMiddleware from my…
Cerin
  • 60,957
  • 96
  • 316
  • 522
2
votes
0 answers

Running Django (v1.9) unit test as given in official tutorials fails with 'Failed to import test module: polls.tests'

I'm following the tutorial given in the Django official website (Django v1.9 and python3). For executing the test in polls\tests.py the import to polls.test is failing. E:\Django official tutorial\mysite>python manage.py test polls Creating test…
Rajasi Kulkarni
  • 111
  • 1
  • 11
2
votes
3 answers

Is there any way to get the default domain of Client() in unittest of Django?

I would like to get the default domain name of Client() in Django unittests. I saw a way to change the default one. But didn't find a way to get the default domain name.
Vipul Vishnu av
  • 486
  • 1
  • 5
  • 15
2
votes
1 answer

rest-framework permission testing

I am working with the latest django-rest-framework and want to create some tests. I have a ModelViewSet and a custom permission which accesses request.GET. This all works well, but in my unittest the GET dictionary is empty. Here is my code: class…
ilse2005
  • 11,189
  • 5
  • 51
  • 75
2
votes
0 answers

Django Testing: Objects from setUp not found

I have this test in my Django 1.7 project: class DummyTestTest(TestCase): def setUp(self): self.test_user = User.objects.create(username='tester', first_name='test', last_name='er', password="1234", email="foo@bar.com") def…
Lukas Barth
  • 2,734
  • 18
  • 43
2
votes
1 answer

Unittest.TestCase Method to Verify Arrays are of Equal Length

How do I pass variables declared from the python command line to the testcase instance? Edit: a and b are inputs to the method func(). a = [1,2,3] b = np.array([1,2]) Filename: code.py import unittest import numpy as np def func(a,b) c = a*b …
MyopicVisage
  • 1,333
  • 1
  • 19
  • 33
2
votes
1 answer

Django Unitest Checking Value Of Template Variable

Suppose I have {{registered}} variable in template. I wrote a piece of test: def nice_test(): response = self.client.post(reverse('app:register'), {'username': 'dupa'} and there I want to check value of variable registered in response. How to…
sweet_sugar
  • 1,390
  • 3
  • 13
  • 22
2
votes
0 answers

Django Test Returning Magic Mock and Not Patched Return Value

Have a report I am mocking out. The report is returned as a dictionary - each metric in the report is a key and the value is an array of n length (number of days in the report). In the case that no data was returned from the report an array of 0s is…
Lorena Nicole
  • 499
  • 2
  • 9
  • 21
2
votes
2 answers

Running Django unittests causes South migrations to duplicate tables

How do you prevent Django unittests from running South migrations? I have a custom Django app, myapp, that I'm trying to test with manage.py test myapp but when I run it I get the error: django.db.utils.OperationalError: table "myapp_mymodel"…
Cerin
  • 60,957
  • 96
  • 316
  • 522
2
votes
0 answers

speeding up django tests with large data sets

I have been having issues with very slow unit test run throughs (2 hours +) in django with large data sets so wrote the following to enable faster testing, which got the time down to 40 mins by replacing all of our fixture loading tests with this…
stillwedge
  • 21
  • 3
2
votes
1 answer

I'm a bit confused about running django tests

So I followed the instructions on the site here: https://docs.djangoproject.com/en/1.5/topics/testing/overview/ but what confuses me is the portion that describes the scope of tests when running. It says: By default, this will run every test in…
Jared Joke
  • 1,226
  • 2
  • 18
  • 29
2
votes
1 answer

Getting user logged in with django-social-auth

I set up a unit test as such: class UserViewTests(TestCase): def setUp(self): self.user_passwords = '123456' self.user1 = User.objects.create(username='Bobby Johnson', password=self.user_passwords) ... def…