Questions tagged [django-tests]

Use for questions about testing applications based upon the Python web framework Django, or the features provided in the module itself (django.test)

docs: https://docs.djangoproject.com/en/2.1/topics/testing/overview/

450 questions
6
votes
1 answer

Can't invoke celery task in Django tests synchronously

I'd like to invoke celery tasks synchronously during my Django tests without need to run celery worker. To achieve this I have specified CELERY_ALWAYS_EAGER=True in my settings.py but it doesn't seem to work. So I decided to apply override_settings…
mateuszb
  • 1,083
  • 10
  • 20
6
votes
4 answers

Django Testing - access session in RequestFactory

I am using RequestFactory in a Django test, and I can't find the right way to access the session variable, and I'm getting the following error when I try self.factory._session["zip_id"] or self.factory.session["zip_id"].…
user1187968
  • 7,154
  • 16
  • 81
  • 152
6
votes
6 answers

How can we make Django tests faster?

We are using Django 1.4 with PostgreSQL on Ubuntu 12.04. We have many tests, and the problem is that running the tests is very slow, I think because for each test the database is created from scratch. I want to make the tests faster by running them…
Uri
  • 2,992
  • 8
  • 43
  • 86
6
votes
2 answers

Test a custom filter in admin.py

I've the following filter in my admin.py file: class parentCategoryFilter(admin.SimpleListFilter): title = 'parent category' parameter_name = 'parent_category' def lookups(self, request, model_admin): first_level_categories =…
Ben
  • 16,275
  • 9
  • 45
  • 63
5
votes
1 answer

Django Test Redirect to login with next parameter

I'm trying to Test non-login user redirect to login url. Is there a way to pass get parameter (example 'next') to reverse_lazy? I'm using class-based views. class SecretIndexViewTest(TestCase): url = reverse_lazy('secret_index') login_url =…
XJOJIX
  • 117
  • 2
  • 7
5
votes
1 answer

Unit Test: How To Mock MEDIA_ROOT But Still Access A File That Is Stored In My Normal MEDIA Folder

Im testing one of my webpages POST functions. if request.method == 'POST' it generates a pdf and attaches it to the user. Every time I ran tests I was generating pdf files which were building up in my MEDIA folder and I had to manually delete them.…
horse
  • 479
  • 7
  • 25
5
votes
1 answer

How to test api that uses django-rest-auth token authentication?

I am using django-rest-auth for authentication and using token provided by it for authorization. I have use some permission_class also provided by django-rest. I have folloging in views.py before my each…
5
votes
0 answers

Django 2.0 - How to deal with testing on only one database when we have several?

First of all, I'm new to asking question here, so bear with me if I don't do it perfectly :) Here goes... I am setting up a Django2.0 API that is going to deal with statistics. It has its own 'default' database to write stuff obviously, but I am…
5
votes
1 answer

Python unittest task.apply_async()

Using coverage to look what has to be tested, and coverage shows that next to this has to be tested: send_alert.apply_async() I know it is celery task, but is there any way to test the line of code? rest of code to understand logic for test: class…
Beliaf
  • 577
  • 2
  • 8
  • 25
5
votes
2 answers

django 1.8 tests with models and migrations

I use django 1.8 I have a model with reference to django.contrib.contenttypes.ContentType: from django.db import models from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import…
Shi3A
  • 71
  • 4
5
votes
1 answer

Django, Testing Redirects to External Pages

I have a routing rule in my Django app for downloads that redirect to a external CDN. I am now writing tests for my app, and I want to test that the route does successfully redirect to the configured url in my Django settings. Here is a simplified…
macguru2000
  • 2,042
  • 2
  • 18
  • 27
4
votes
1 answer

RuntimeError: Model doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

I am writing an app in Django and I'm trying to do some unit testing but I can't seem to find why the test is failing that is the test page: import re from django.test import TestCase from django.urls import reverse from . import models class…
4
votes
1 answer

Unit test for Django Update form

I do not understand how to manage updates on forms and related unit tests and I would really appreciate some advises =) I have a Company model, and related very simple CompanyForm: class Company(models.Model): """ Company informations -…
Christophe
  • 651
  • 4
  • 22
4
votes
2 answers

Django is always returning 404 while testing. "The requested resource was not found on this server."

I have an Django API that I build quickly, and now I want to add some tests to ensure is stable. The problem is that Django simply refuses to let me access any resource on the server. It can't find any URL. I have some models, but this test is with…
4
votes
1 answer

How To Create A Temp File Structure For Testing

During unit testing and Selenium functional testing files are generated which are not cleaned up afterwards. I have spent a very long time trying to get django-cleanup working, but it just does nothing. I figure I need to create a temporary file…
horse
  • 479
  • 7
  • 25
1 2
3
29 30