1,226 questions
0
votes
0
answers
30
views
Flaky Circle CI tests (django): ImportError: cannot import name "task" from "app.tasks" (unknown location)
Sometimes, I have many flaky test failures due to one error:
ImportError: cannot import name 'task_import_events_to_db' from
'app.tasks' (unknown location)
It seems the tests fail because of this ...
0
votes
1
answer
40
views
Django Unit Test - using factory_boy build() on a Model with Many-To-Many relationship
I’m working on writing unit tests for a DRF project using pytest and factory_boy.
I’m running into issues with many-to-many relationships. Specifically, when I try to use .build() in my unit tests, ...
1
vote
1
answer
62
views
CheckConstraint in Django model not triggering in unittest.TestCase (AssertionError: IntegrityError not raised)
I have a Model class with a series of constraints that I am attempting to test, and I am unable to get these constraints to return an IntegrityError in testing. The class is as follows:
from django.db ...
0
votes
0
answers
57
views
Getting attribute error in Django BaseCommand- Check
I am working on a tutorial project. The same code works for the instructor but doesn't work for me.
I have a file for custom commands:
import time
from psycopg2 import OperationalError as ...
0
votes
1
answer
80
views
Django Testing: Use main database as only one database available?
I am a university student, and I decided to use Django for my final year project. This means I am limited to using the University's MySQL database server. On the server, I am only allowed to have one ...
0
votes
1
answer
54
views
django.db.utils.ProgrammingError: (1146, "Table 'test_db.sensori' doesn't exist")
i have this problem when i run my testcase with django and mysql.
when i run manage.py test i've got this error.
Found 1 test(s).
Creating test database for alias 'default'...
System check identified ...
0
votes
0
answers
17
views
Django LiveServerTestCase live server shuts down prematurely
I've been trying to run some functional tests (LiveServerTestCase) with Selenium for a Django app. For demonstration it's a note taking app. When I'm trying to simulate note creation, it requires ...
0
votes
3
answers
63
views
Django test client redirects logged users to login page
I know this questions has already been asked, but none of the answers works for me.
Whenever I try to use the client login in Django tests, it basically gets ignored and I get a redirect to the login ...
2
votes
2
answers
485
views
Configuring Django Testing in PyCharm
I have a simple django project that I'm making in pycharm. The directory structure is the following:
zelda_botw_cooking_simulator
|-- cooking_simulator_project
|---- manage.py
|---- ...
0
votes
0
answers
43
views
Importing login of users/tests module to another application/tests not working in Django
I'm trying to avoid code duplication so I want to import login from users/test to the ordering/test where I need to test different views that have @login_required.
users/test.py
User = get_user_model()...
-2
votes
1
answer
135
views
A more elegant approach to writing Django’s unit tests
I am currently writing tests using Django’s unit tests (based on Python standard library module: unittest). I have written this test for my Contact model which passes:
class ContactTestCase(TestCase):
...
1
vote
1
answer
41
views
How do I pass request to a form in test
I am trying to pass request to a Django form in unittest. This is my test:
def test_AddPairedStudyForm(self):
self.client.force_login(self.user)
request = RequestFactory().get(reverse("...
0
votes
1
answer
58
views
In python Django how to define test database and keep records inserted in test database until cleaned in tearDown method of testcase
I want a test database created for my default database in Django latest version, for that I configured in project settings.py file as below.
DATABASES = {
'default': {
'ENGINE': 'django.db....
0
votes
1
answer
37
views
Defining test methods in base class which should not be instanciated directly
I am trying to create test cases which only differ by which user is logged in.
Therefore I figured defining the tests in a base class and creating sub classes which log in the user during setUp() ...
1
vote
1
answer
56
views
Build a pre-decorated class with override_settings for faster client.login?
This makes a Django test using the test client run very much faster, where login security is not an important part of the test
from django.test import TestCase, Client, override_settings
@...
2
votes
0
answers
423
views
Django- How to use django-silk to work in running tests
I use the django-silk to profile the APIs of my Django project (Django + DRF). Currently, I call APIs in the browser (using DRF browsable API) and then I can see the API profiling data in the silk. ...
2
votes
2
answers
204
views
Django test suite throws error on field that doesn't exist in latest migrations
The following simple test,
from django.test import TestCase
class TestSetup(TestCase):
def test_setUp(self):
pdb.set_trace()
# Code here deleted, it made no ...
0
votes
0
answers
34
views
AssertionError in Django Unit Test: Unable to Retrieve Correct Count from Database
I'm encountering an AssertionError in my Django unit test when attempting to save a POST request to an existing list. Despite creating the list and sending the POST request successfully, the test ...
0
votes
1
answer
67
views
How to test a view in Django
I am learning basics of Django following official tutorial and adding some new features to my application. So I added a view that can nullify all votes of particular question
def nullask(request, ...
1
vote
1
answer
288
views
Django SimpleUploadedFile is not being sent
I'm developing Django application and wrote a test that checks if view method acts correctly. I need to send some data in request, including the image file.
In my case:
def setUp(self):
...
self....
0
votes
2
answers
115
views
When I run python manage.py test... only some of my tables / fields are created in the test db
In django, I am trying to run tests on a test db, but I can't seem to get the test db stood up properly...
I have 226 models on my real db, but when I run python manage.py test (test location) it ...
3
votes
1
answer
221
views
Django Tests - TemplateSyntaxError: 'socialaccount' is not a registered tag library while running tests
I'm trying to run Django tests but I'm encountering with this error - django.template.exceptions.TemplateSyntaxError: 'socialaccount' is not a registered tag library., but normally it have to work ...
0
votes
2
answers
150
views
DRF post request test failing because a custom permission is stating that the "owner_id" field (custom field) does not match the authenticated user id
I'm starting to write tests for my endpoint, "categories/", and I can't get past a custom permission that I have added to the view that is being tested. In this permission, it checks the ...
0
votes
1
answer
110
views
How do I test ExportActionMixin in Django?
I'd like to test files produced on the admin website by import_export.admin.ExportActionMixin in Django.
In particular I'd like to test the admin command by looking into the file (which I download on ...
0
votes
1
answer
449
views
Run Tests with Coverage "TypeError: Parameters to Generic[...] must all be type variables"
When running tests with coverage using PyCharm on django (python) project, im having the following error:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2023.1.2\...
1
vote
1
answer
122
views
Test post django function
I have django function, that takes JSON-data from request, validates it and save to db.
I want to test this func, but POST request doesn’t work, error
‘json.decoder.JSONDecodeError: Expecting value: ...
1
vote
0
answers
619
views
How to serve InMemoryStorage media files during Django tests
I have some Selenium-based integration tests (subclassed under django.contrib.staticfiles.testing.StaticLiveServerTestCase) for my Django app that require a functioning user-uploaded media ...
0
votes
1
answer
101
views
I want to add a DRF API route only for testing (override_settings), getting 404
I want the following test to pass, but I am always getting a 404 Error. But I would expect the "get all" request to return all users.
import json
from django.test.utils import ...
1
vote
0
answers
331
views
Django test parallel fails while clone database
I have weird behavior of Django tests in parallel mode: Command to run tests: python manage.py test --parallel
Sometimes it works fine but in random moments I got error during cloning DB.
Cloning test ...
0
votes
1
answer
50
views
Data missing (files) in my django serializer
I have an issue, some data are missing in my serializer (in validate and create functions).
My Model:
class PostsImage(models.Model):
image = models.ImageField(upload_to="posts/images")
...
0
votes
1
answer
1k
views
psycopg2.errors.DuplicateTable: relation "app_appfile" already exists
I use postgres as DB for my project. I would like to make and run some test in my test.py file with sqlite DB. I change my settings.py file to do that with sqlite3 db, but when I try to run my simple ...
2
votes
2
answers
1k
views
How can I see the body of the request that Django's test client makes
I am writing automated tests in Django and started writing tests for an API endpoint that supports the put method.
I am making my put request like so:
response = self.client.put(path, data=json.dumps({...
0
votes
0
answers
292
views
Python - Test requests with retry and timeout
I have the following piece of Python code and I would like to write tests for this function:
def http_request(method, url)
session = requests.Session()
retries = Retry(total=3, backoff_factor=...
1
vote
1
answer
32
views
Tests - both classes share the same login code but it only works in one
I'm writing tests for locallibrary app following the tutorial on MDN. Both classes that test views share similar login code, but it only works in one class
I tried finding spelling errors and comping ...
1
vote
1
answer
158
views
How Django runs tests parallelly?
I'm curious how django runs test parallelly with
python manage.py test --parallel.
Would someone please explain the what happens under the hood and and the the life cycle of test with an example.
1
vote
1
answer
483
views
Adding custom header to DRF test APIClient
I need to add a custom header to the APIClient for a test.
I have tried to follow this section of the official docs
from rest_framework.test import APIClient
client = APIClient()
client.credentials(...
1
vote
1
answer
703
views
django tests - error in github action while running in parallel
in a github action yml file it's defined this step to run django tests:
python manage.py test --failfast --parallel 2
looks like --parallel 2 breaks it:
multiprocessing.pool.RemoteTraceback:
"&...
0
votes
1
answer
235
views
How to send POST request with query params for image field in django tests
I have test.py
from rest_framework.test import APITestCase
from django.core.files.uploadedfile import SimpleUploadedFile
from django.urls import reverse
class PostImageTest(APITestCase):
def ...
1
vote
2
answers
146
views
Return fake dates for every blog post - bypass django's auto now
I am testing whether my blog posts are in reverse chronological order. To do so, I must set random dates for each post created. I'm using faker to set the dates. I am getting back a fake date, but it'...
0
votes
1
answer
55
views
"Select a valid choice. That choice is not one of the available choices" error on OneToOneField
models.py
class Major(models.Model):
major = models.CharField(max_length=50, unique=True)
description = models.TextField()
image = models.ImageField(default='default_pics/default_major.jpg'...
1
vote
1
answer
621
views
Django: django.urls.exceptions.NoReverseMatch: Reverse for 'view_name' not found. 'view_name' is not a valid view function or pattern name
I'm trying to run a test for my view and I get this error:
Django: django.urls.exceptions.NoReverseMatch: Reverse for 'create' not found. 'create' is not a valid view function or pattern name.
Here is ...
1
vote
0
answers
148
views
Unable to route to test pages using Wagtail Factories and Factory Boy
I'm trying to implement factory boy and wagtail factories to generate tests for the blog section of my site, at /blog/, which uses Wagtail CMS. However, I'm getting a 404 error when doing self....
0
votes
1
answer
129
views
Django: color test result in console
I have a Django project for which I have tests (nothing special).
The thing is I'd like to have colored results when running tests and I don't know how to do that.
On a daily basis, I run tests from ...
1
vote
3
answers
819
views
Why is the page I am testing returning a 301 response instead of a 200 response?
I am writing tests for class-based views in the django project I am writing as part of a course. Currently, I am stuck trying trying to test a view that directs to an edit page that requires a user to ...
0
votes
2
answers
252
views
I am new to django and testing and I'm trying to test a registration and login view
The view has IsAdminUser restriction. I am also using token authentication.
This is the view:
class UserRegister(generics.CreateAPIView):
permission_classes = [IsAdminUser]
queryset = User....
0
votes
1
answer
441
views
Override test settings from a file
For context, when one wants to change settings in a test (yes, it's ok to change it there), we can use override_settings() or modify_settings() (as observed here). That works when running the tests in ...
1
vote
1
answer
401
views
Getting HttpResponseNotFound in Django test get method
I am building a test case for an endpoint. Visiting the endpoint in the local development using the browser works fine -- I get the expected response. However, during the test, I get an ...
0
votes
1
answer
218
views
How to create test bucket before running tests by django-minio-backend
I use django-minio-backend in order to integrate Django and Minio.
I want to create a test bucket for created media files during tests running.
I tried to do that in a custom test runner as follows:
...
0
votes
1
answer
69
views
Django Rest Framework Testing
I have a LoginSerializer that has the block of code as below
def validate(self, attrs):
username = attrs.get('username', '')
password = attrs.get('password', '')
user = auth.authenticate(...
3
votes
2
answers
746
views
Disable logging bad requests while unittest django app
I have a tests in my Django app. They're working well, but i want to disable showing console logging like .Bad Request: /api/v1/users/register/
One of my tests code
def ...