Django Unittests Client Login: fails in test suite, but not in Shell

2024/9/20 9:20:44

I'm running a basic test of my home view. While logging the client in from the shell works, the same line of code fails to log the client in when using the test suite.

What is the correct way to log the client in when using the Django test suite?

Or

Any idea why the client is not logging in with my current method?

Shell test:

import unittest
from django.test.client import Client
from django.test.utils import setup_test_environment
setup_test_environment()client = Client()
login = client.login(username='agconti', password='password')# login --> evals to True

Django test suite:

#### View Tests ####
import unittest
from django.test.client import Client
from shopping_cart.models import Store, Order, Item, Transactionclass Home_ViewTest(unittest.TestCase):def setUp(self):self.client = Client()# login userlogin = self.client.login(username='agconti', password='password')# Check loginself.assertEqual(login, True)def test_details(self):# get the home viewresponse = self.client.get('/')      self.assertEqual(response.status_code, 200)# Check that the rendered context is not none self.assertTrue(response.context['stores'] != None)# self.assertEqual(login, True) --> login evals to False, raises the assertion
Answer

A test user should be created. Something like:

  def setUp(self):self.client = Client()self.username = 'agconti'self.email = '[email protected]'self.password = 'test'        self.test_user = User.objects.create_user(self.username, self.email, self.password)login = self.client.login(username=self.username, password=self.password)self.assertEqual(login, True)

In your shell test the user is probably already present in your db, whereas in your unittest there might not be a user...

https://en.xdnf.cn/q/72186.html

Related Q&A

Icon overlay issue with Python

I found some examples and topics on this forum about the way to implement an icon overlay handler with Python 2.7 & the win32com package but it does not work for me and I dont understand why. I cre…

Comparing NumPy object references

I want to understand the NumPy behavior.When I try to get the reference of an inner array of a NumPy array, and then compare it to the object itself, I get as returned value False.Here is the example:I…

Does using django querysets in templates hit the database?

Do template value tags force django to hit the database when called against a non-context value? For example:{{ request.user.username }} Is the call to show the currently logged in users username. H…

how to randomly sample in 2D matrix in numpy

I have a 2d array/matrix like this, how would I randomly pick the value from this 2D matrix, for example getting value like [-62, 29.23]. I looked at the numpy.choice but it is built for 1d array.The f…

How to update figure in same window dynamically without opening and redrawing in new tab?

I am creating a 3D scatter plot based off a pandas dataframe, and then I want to re-draw it with slightly updated data whenever the user presses a button in my program. I almost have this functionality…

Serializing a C struct in Python and sending over a socket

Im trying to serializing the following C structstruct packet {int id;unsigned char *ce;unsigned char *syms; };in Python and send it over a socket. The number of elements pointed by ce and syms are know…

creating multiple audio streams of an icecast2 server using python-shout

I am trying to create a web radio server to stream 3 sources at once. I am using python to create a source client for icecast2 using the python-shout library. I am not too familiar with the language (p…

Custom Deployment to Azure Websites

I recently started using Gulp.js to package all my CSS and JavaScript into single files, which I then include in my web app. My web app is written in Python (using Flask).I obviously dont want to track…

Python - Gspread Request Error 401

Im currently making a Discord-bot that connects to a Google-spreadsheet (gspread). But after Ive been running it for a while it starts to hand out errors and it cant connect to my gspread anymore (unle…

paramiko server mode port forwarding

I need to implement a ssh server using paramiko that only handles -R port forwarding requests like this:ssh -N -T -R 40005:destination_host:22 [email protected]So far from what i understand ill have to…