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
9
votes
5 answers

Django Unit Tests - Unable to create the django_migrations table

I'm trying to write some unit tests and run them using manage.py test but the script can't create the django_migrations table for some reason. Here is the full error: Creating test database for alias 'default'... Traceback (most recent call last): …
ravioli
  • 3,749
  • 3
  • 14
  • 28
9
votes
3 answers

Unit testing a Django query set

I'm trying to learn unit testing with Django/unittest. These are simple versions of my models: class Device(models.Model): name = models.CharField(max_length=100) def get_ips(self): return…
Vaughan
  • 391
  • 5
  • 18
9
votes
2 answers

How to debug Django unit tests?

I have a Django unit test, and I want to put print statements in it for debugging purposes but apparently django is suppressing them. How else can I print the value of a variable? I may be able to hack around with asserts, etc. but wondering how…
Nima
  • 854
  • 3
  • 11
  • 18
9
votes
1 answer

Overriding decorator during unit test in python

I have a django class based view that I'm decorating. Unfortunately that decorator makes outside calls to do status checks which is outside the scope of what the unit test should do so I want to override the decorator to do nothing during my unit…
John P
  • 720
  • 2
  • 8
  • 16
8
votes
1 answer

How do I test django with gitlab ci

I have a gitlab-ci server that works fine and can connect with my gitlab server,Please can any one point me in the right directing on how to run my django unittest/builds using gitlab-ci runner thanks.
user1940979
  • 895
  • 1
  • 13
  • 29
7
votes
2 answers

Testing for expected 404 with Django test client leads to unhandled exception

I'm using the Django test client, django.test.client.Client, to test some views in a Django app. In particular, I'm testing a case where the view calls a get_object_or_404 method and the object isn't there, so a 404 should be returned. My test code…
Lorin Hochstein
  • 57,372
  • 31
  • 105
  • 141
7
votes
1 answer

How to correctly make an assertLogs test with unittest using a django settings configuration for logging?

I have a setting for logging in my django project and I want to make a test using assertLogs. I used the example provided in the documentation: https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertLogs with self.assertLogs('foo',…
Manuel Carrero
  • 599
  • 1
  • 9
  • 29
7
votes
3 answers

Django executing tests for app not in INSTALLED_APPS

Under my Django project there are a few apps and all of them have unit tests. One of them that I'm working right now is supposed to be included only in dev/stage environments, so I'm enabling it using a environment variable. When this variable is…
Gabriel
  • 71
  • 1
  • 2
7
votes
1 answer

Mocking forms in view unit tests

I can't seem to be able to mock the behaviour of a form when unit testing views. My form is a simple ModelForm and resides in profiles.forms. The view is (again) a simple view that checks whether the form is valid and then redirects. views.py from…
Mathieu Dhondt
  • 8,405
  • 5
  • 37
  • 58
7
votes
1 answer

How to write unit test for admin save function

I have customized save_model admin i.e. class MyModelAdmin(admin.ModelAdmin): def save_model(self, request, obj, form, change): # some more question code here obj.save() Now, I would like to test MyModelAdmin save_model…
Elisa
  • 6,865
  • 11
  • 41
  • 56
7
votes
3 answers

python unit test: assertEqual on same objects throwing AssertionError

I have a class as class PlaylistManager(models.Manager): def add_playlist(self, name): playlist = Playlist(name=name) playlist.save() return playlist def get_playlist_with_id(self, id): return…
daydreamer
  • 87,243
  • 191
  • 450
  • 722
6
votes
1 answer

Got an error creating the test database: Django unittest

When I run python manage.py test, django is asking a strange question each time: $ python manage.py test Creating test database for alias 'default'... Got an error creating the test database: (1044, "Access denied for user 'nyble'@'localhost' to…
codyc4321
  • 9,014
  • 22
  • 92
  • 165
6
votes
1 answer

Django testing the html of your homepage against the content of a response

If I have a test like so... def test_home_page_returns_correct_html(self): request = HttpRequest() response = home_page(request) expected_html = render_to_string('home.html', request=request) …
Reiss Johnson
  • 1,419
  • 1
  • 13
  • 19
6
votes
2 answers

How to unittest a django database migration?

We've changed our database, using django migrations (django v1.7+). The data that exists in the database is no longer valid. Basically I want to test a migration by, inside a unittest, constructing the pre-migration database, adding some data,…
John Mee
  • 50,179
  • 34
  • 152
  • 186
6
votes
3 answers

Django 1.8 - How do I test a specific file inside a tests folder?

This is my directory: CMSApp/tests/test_page.py CMSApp/tests/test_user.py CMSApp/models.py CMSApp/views.py I want to test test_page.py only. I can do this: python manage.py test CMSApp/tests But that will test both test_page.py and test_user.py.…
SilentDev
  • 20,997
  • 28
  • 111
  • 214
1
2
3
21 22