django-testing

Override test settings from a file

Override test settings from a file Question: 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 parallel too. Generally speaking, I do something like this from django.test import TestCase, override_settings …

Total answers: 1

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

Extend social pipeline and prevent a specific function to run during tests

Extend social pipeline and prevent a specific function to run during tests Question: I’m using Python Django Social Auth and extended the pipeline with the following three steps One before the user is created (partial pipeline) requesting some data. One for the user creation (overrides the social.pipeline.user.create_user method). One after the user is created. Here’s …

Total answers: 1

Django test for 404 erroring because Client expects 200?

Django test for 404 erroring because Client expects 200? Question: I am trying to test my 404 page to ensure certain elements are present on it. My test looks like this: class TestApp404PageIncludesLink(TestCase): def setUp(self): superuser = UserFactory(is_superuser=True, is_staff=True) self.client.force_login(superuser) def test_superuser_can_see_link(self): response = self.client.get("404") self.assertTrue(response.status_code == 404) self.assertContains(response, ‘href="/special_link/">Specialty</a>’) I am running this test …

Total answers: 2

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

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

How to write unit test for user-detail with router.register base name

How to write unit test for user-detail with router.register base name Question: This error appears when I write a test for user-detail, I thought the error from response = self.client.get(reverse(‘user-detail’,kwargs={‘pk’:1})), I used router.register to config ulrs base name, and that makes me confused when writing the test. So in this case, where the point in …

Total answers: 2

How can I post data into test Client post request?

How can I post data into test Client post request? Question: I’m writing a test to see if form data is validating on post request trying to create a Post object. tests.py def setUp(self): user = User.objects.create_user(email=’[email protected]’, password=’test’, name=’test’) self.user = User.objects.get(email=’[email protected]’) self.client.login(email=user.email, password=user.password) @tag(‘fast’) def test_index(self): client = Client() response = client.post(reverse(‘index’), data={‘user’: self.user, …

Total answers: 3

What are the differences between setUpClass, setUpTestData and setUp in TestCase class?

What are the differences between setUpClass, setUpTestData and setUp in TestCase class? Question: More specifically, what are the use cases for each? What I’ve understood so far: setUpClass This method runs once, before all the tests in a test class setUpTestData This method runs once if the DB has transaction support. Otherwise it runs before …

Total answers: 1

Issue adding request headers for Django Tests

Issue adding request headers for Django Tests Question: I need to add a header to a request in a Django test. I have browsed a few pages on both Stack Overflow and elsewhere, following various suggestions. I can add the headers to PUTs and GETs successfully, but having an issue with POSTs. Any advice would …

Total answers: 2