Opened 6 months ago

Last modified 6 months ago

#21357 new Bug

Test client session does not behave as stated in the django documentation.

Reported by: aaronmerriam@… Owned by: nobody
Component: Testing framework Version: master
Severity: Normal Keywords:
Cc: bmispelon@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


In the documentation for testing, it states that you can run the following code to manually insert values into the test client session.

def test_something(self):
    session = self.client.session
    session['somekey'] = 'test'

In actuality, self.client.session is of type dict and a calling save() raises an AttributeError.

Attachments (0)

Change History (3)

comment:1 Changed 6 months ago by bmispelon

  • Cc bmispelon@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 1.5 to master

I can reproduce this issue.

It looks like the nature of client.session can depend on the settings [1] which might be what's causing the discrepancy here.

I'm not sure if that's indicative of a bug in the test client (maybe it should return something other than a plain dict if contrib.sessions is not installed) or if it's a documentation issue (in which case it should be mentionned that the given code only works with contrib.sessions installed).

In any case, I'm marking this as accepted.

Thanks for the report.


comment:2 Changed 6 months ago by Harm Geerts <hgeerts@…>

There also seems to be a regression in that code from the commit

>>> from django.test.client import Client
>>> client = Client()
>>> client.session
>>> client.session['var'] = 'val'
>>> client.session

A test case which tested the above used to exist but was removed in

comment:3 Changed 6 months ago by prestontimmons

I added a pull request with a fix for this:

For reference, this issue was reported in #15740. That was marked as a duplicate of #10899, but I no longer think that is correct. While the feature in #10899 would fix this case, the behavior it tries to add is more clever than it needs to be.

Add Comment

Modify Ticket

Change Properties
<Author field>
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.