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
3
votes
1 answer

How to write unittests for Consumers in django channels?

A'm beginner in django-channels. I'm use channels 2.3.1. I have following code. main routing websocket_urlpatterns = [ url(r'^ws/events/(?P[^/]+)/(?P[^/]+)/$', consumers.Consumer, name='core-consumer'), ] app…
unknown
  • 252
  • 3
  • 12
  • 37
3
votes
1 answer

How can I start django unittest in VSCode?

I'm trying to run django unittest using VSCode, not terminal. my project tree looks like this: ├── db.sqlite3 ├── hero │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-37.pyc │ │ ├── admin.cpython-37.pyc │ │ ├──…
3
votes
1 answer

How to run Django units test with default and unmanaged database?

I have a Django project with a default database used for storing things like user, orders etc. We also have an unmanaged database. Now when you run Django test they try to make test databases, but since we have an unmanaged db we cannot do this. I…
Serena
  • 185
  • 1
  • 14
3
votes
1 answer

Testing manual transactions inside Django unittests

How do you test code that disables autocommit and uses savepoints, inside a Django unittest? The default Django unittest class wraps all tests inside an @atomic decorator, which is usually exactly what you want, to ensure the sqlite database gets…
Cerin
  • 60,957
  • 96
  • 316
  • 522
3
votes
2 answers

Unit Testing Django Model Save Function

I'm creating tests to check that a custom calibration model save function updates an asset record (foreign key) if it is the latest calibration record for the asset. The save function performs exactly as expected in live dev & production server and…
3
votes
0 answers

Django Unit test : `freeze_time` not work well in some cases (Seems like a bug..?)

This is my code for testing the role of TIME_ZONE and `USE_TZ: from django.test.utils import override_settings class TimeZoneTest(TestCase): @override_settings(TIME_ZONE='UTC', USE_TZ=True) @freeze_time("2018-01-04 13:30:55", tz_offset=9)…
user3595632
  • 5,380
  • 10
  • 55
  • 111
3
votes
1 answer

Writing a Django unit test for a function with a HttpResponse

I'm writing a unit test to check to see if the telecasts key is in the JSON data returned in this function (in my views.py): def my_function(request, date1='', date2='', date3='', date4=''): ::some other functions...:: return…
NewToJS
  • 2,011
  • 4
  • 35
  • 62
3
votes
1 answer

Setting pycharm run django unittest

I can not run Django Python pytest under PyCharm After I got the answer from the above URL. I am now trying to fully utilize my IDE functions as much as I can. Here is another project in my company that using Django UnitTest. This is the command…
joe
  • 8,383
  • 13
  • 61
  • 109
3
votes
0 answers

foreign key constraint violation with RelatedFactory in django unit tests

I have a OneToOne model which I use with a Factory in unit tests. When tests are executed I get a foreign key constraint violation: IntegrityError: insert or update on table "core_designfeatures" violates foreign key constraint This wasn't a…
mikebz
  • 3,277
  • 8
  • 37
  • 50
3
votes
1 answer

Testing for a ValidationError in Django

If I have a model called Enquiry with an attribute of email how might I check that creating an Enquiry with an invalid email raises an error? I have tried this def test_email_is_valid(self): self.assertRaises(ValidationError,…
Reiss Johnson
  • 1,419
  • 1
  • 13
  • 19
3
votes
1 answer

How to mock a specific method of a class based View without overriding `self`?

I am trying to patch a function of a view within a TestCase in django. I have a class: class BaseView(View): def _get_data(self): self.attribute = self.fancy_stuff() self.thing = self.do_other_stuff() I need to mock out…
codyc4321
  • 9,014
  • 22
  • 92
  • 165
3
votes
1 answer

Unit testing django application error creating database table OperationalError: (1005, "Can't create table...)

I am trying to unitest my models in my django application that consisits of some migrations. I have prepared my test, but when running ./manage.py test my_app i get the following error django.db.utils.OperationalError: (1005, "Can't create table…
Apostolos
  • 7,763
  • 17
  • 80
  • 150
3
votes
0 answers

How do you register a custom management command in a Django unittest

How do you register a custom Django management command that only exists within a unittest? In Django 1.5, you could do this like: from django.test import TestCase from django.core.management import _commands from myapp.testse.commands import…
Cerin
  • 60,957
  • 96
  • 316
  • 522
3
votes
2 answers

Testing Django view requiring user authentication with Factory Boy

I need a view that allows staff users to view objects in a draft state. But I'm finding it difficult to write a unittest for this view. I'm using Factory Boy for my setup: class UserFactory(factory.django.DjangoModelFactory): class Meta: …
Patrick Beeson
  • 1,667
  • 21
  • 36
3
votes
1 answer

django tests response.request.user.is_authenticated() returning True after logout

I am trying to write some tests for the authentication part of my application and I encountered a problem with checking if the user is logged in or not. Here's the code: client = Client() # user signup form response =…
linoor
  • 211
  • 1
  • 4
  • 12