django-tests

Django testing fails with object not found in response.context even though it works when actually running

Django testing fails with object not found in response.context even though it works when actually running Question: I’m trying to test if my PlayerPoint model can give me the top 5 players in regards to their points. This is the Player model: class Player(AbstractUser): phone_number = models.CharField( max_length=14, unique=True, help_text="Please ensure +251 is included" ) …

Total answers: 2

Django unittest function with redirect based on mock return_value

Django unittest function with redirect based on mock return_value Question: I have a view function similar to def my_function(request): session = create_something(‘some_random_string’) return redirect(session.url, code=303) To test it import unittest from django.test import TestCase from unittest.mock import patch from my_app.views import my_function class TestMyFunction(TestCase): @patch(‘my_app.views.create_something’, return_value={ "url": "https://tiagoperes.eu/" }) def test_my_function(self, mock_create_something): response = self.client.get("/my-function/") …

Total answers: 1

Django Unit Tests – Changes to self properties are persisting from test to test

Django Unit Tests – Changes to self properties are persisting from test to test Question: I have a test class that has a setup like this. We import a dictionary from a test_helpers file that has some default data. The data is set as FORM_DATA[‘value_A’] = 0 and FORM_DATA[‘value_B’] = 1000000 from the start. Whether …

Total answers: 1

Django TestCase check ValidationError with assertRaises in is throwing ValidationError

Django TestCase check ValidationError with assertRaises in is throwing ValidationError Question: I have a model where i have overridden save function something like: class MyModel(models.Model): number = models.PositiveIngeter() def save(self,*args, **kwargs) if self.number > 10: super().save(*args, **kwargs) else: raise ValidationError(‘msg’) and the function i am testing is like def test_number(self): myModel = MyModel(number=5) self.assertRaises(ValidationError,myModel.save) the …

Total answers: 1

get error class TestInformLogin has no attribute client

get error class TestInformLogin has no attribute client Question: I wrote this code for testing views in django when I run it i get the following error response = self.clinet.get(self.inform_list_url) AttributeError: ‘TestInformLogin’ object has no attribute ‘clinet’ from django.test import TestCase,Client from django.urls import reverse from KnowledgeManagement.models import Members , TblInform class TestInformLogin(TestCase): def set_up_create_user(self): …

Total answers: 3

Pass arguments in django test

Pass arguments in django test Question: I am trying to pass both the user.id and sample.id to a function in the views. Here is my urls.py (url only accepts sample.id ) path(‘<int:tag>’, views.task, name=’task’), Here is my views.py (views.py is looking for user.id but cannot find it) def task(request, tag): task_user = get_object_or_404(Team, id=tag) task_user.assigned_to …

Total answers: 1