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
4
votes
2 answers

Django Unit Test - IDs of created objects

Sample models.py models.py class Food(models.Model): name = models.CharField(max_length=50, verbose_name='Food') def __str__(self): return self.name Suppose that I have written unit test/s: from django.test import TestCase from…
Paolo
  • 823
  • 6
  • 18
4
votes
0 answers

Django Unit Testing in Visual Studio 2017 or 2019:

Django needs setup code so unit tests on models that need a database will work. Standard Django unit test do that silently in the background but not with Visual Studio. I have not found a good way to provide setup code that will work for starting…
4
votes
2 answers

Unit testing Django timezone aware datetime

I tried to compare DRF response and input value. class ViewTest(TransactionTestCase): reset_sequences = True current_date_time = timezone.now() def setUp(self): self.client = APIClient() self.user =…
pyprism
  • 2,928
  • 10
  • 46
  • 85
4
votes
0 answers

Django South fake migration for test database

I started learning how to write unit tests in Django. To run a couple of example tests for an app named, organizations, I typed this in commandline: > python manage.py test organizations.tests which returns migration error like this: Creating test…
user1330974
  • 2,500
  • 5
  • 32
  • 60
4
votes
3 answers

Django testing commit_on_success

I have a function that I've got wrapped in @transaction.commit_on_success and running Django unit tests on it. The function is too long to paste, but some pseudocode is: @transaction.commit_on_success def func(): order = Order.create() …
grokpot
  • 1,462
  • 20
  • 26
4
votes
1 answer

How to use verbosity level in your tests in Django

Running a Django test case allows you to specify verbosity (0,1,2,3) like this manage.py test -v 2 myapp.tests.test_mycode.TestMyCode.test_func How can I receive the verbosity flag inside my test_func
user
  • 17,781
  • 20
  • 98
  • 124
4
votes
2 answers

HttpResponse object becomes string when passed to assertContains

I have a strange problem in a Django template test. When the test executes my view, the view returns an HttpResponse object. However, when I then pass that response object to the Django TestCase assertContains method, the response object becomes a…
Jim
  • 13,430
  • 26
  • 104
  • 155
4
votes
1 answer

Django Unittests Failing Randomly

I am running into this wierd issue of randomly failing unittests. I am basically testing Syndication Feeds Here is the relevant portion of my code: class ObjectFeedTests(PuppyTestCase): def test_object_feeds(self): site =…
karthikr
  • 97,368
  • 26
  • 197
  • 188
4
votes
0 answers

Django TestCase.assertNumQueries - unexpected count for number of queries on object save

I have two models, using multi-table inheritance: # models.py - psuedo-ish code for illustrative purposes class BaseModel(models.Model): name = models.CharField() class SubclassModel(BaseModel): subclass_attr = models.CharField() I'm…
Chris Lawlor
  • 47,306
  • 11
  • 48
  • 68
4
votes
4 answers

Django - testing view which works with ajax requests

I want to test that my view returns correct json after processes. here is my view: @login_required @require_POST def xxx_view(request): if 'post_id' in request.POST: post_id = request.POST['post_id'] post =…
alioguzhan
  • 7,657
  • 10
  • 46
  • 67
3
votes
2 answers

Django Unittest doesn't load fixtures

Django testrunner is not loading fixtures out of media app fixtures/ directory. How can I debug it - find if it's looking for my fixtures and where? (Python 2.7.1, Django 1.3.1) My app (app_label=media) directory…
Memke
  • 684
  • 1
  • 7
  • 24
3
votes
1 answer

Why is Django test cases checking actual DB and raising IntegrityError instead of just running in-memory?

When I run my tests with the DB empty (the actual application DB), everything goes fine. But when the DB has data, Django raises an IntegrityError for basically every test. The stact trace looks like the following (but for every…
Ramon K.
  • 3,402
  • 3
  • 20
  • 29
3
votes
1 answer

Unable to use override_settings class decorator with setUpClass and tearDownClass Django

SOLVED I am trying to write unittests in django. I encountered different behaviours of override_settings decorator when used with setUpClass and tearDownClass classmethods. Following is the code which doesn't work: import logging from django.test…
Sumit Kumar
  • 492
  • 6
  • 13
3
votes
0 answers

Django unit tests spews database error - cannot commit transaction - SQL statements in progress

I'm using unittest2 together with manage.py test, and before it even seems to run any tests, it spews a horrid database error, as below. I'm in my development environment (actually on a dreamhost server for a variety of reasons), using sqlite as my…
Marcin
  • 48,559
  • 18
  • 128
  • 201
3
votes
2 answers

Mocking python kafka producer

I have a file with a function which creates a Kafka Producer and publishes some message on to Kafka topic. def publishToKafkaTopic(value_str): producer = KafkaProducer( bootstrap_servers=KAFKA_SERVERS, value_serializer=lambda x:…
ace
  • 139
  • 2
  • 6