#21357 closed Bug (fixed)
Test client session does not behave as stated in the django documentation.
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Testing framework | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | bmispelon@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
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' session.save()
https://docs.djangoproject.com/en/1.5/topics/testing/overview/#django.test.client.Client.session
In actuality, self.client.session
is of type dict
and a calling save()
raises an AttributeError
.
Change History (7)
comment:1 by , 11 years ago
Cc: | added |
---|---|
Triage Stage: | Unreviewed → Accepted |
Version: | 1.5 → master |
comment:2 by , 11 years ago
There also seems to be a regression in that code from the commit https://github.com/django/django/commit/36b164d838c3de168defe9f1ebc02ea1abc790be
>>> 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 https://github.com/django/django/commit/a935e834433cfc691882c2bbf1249a39abecd35b
comment:3 by , 11 years ago
I added a pull request with a fix for this:
https://github.com/django/django/pull/1832
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.
comment:4 by , 11 years ago
Has patch: | set |
---|---|
Patch needs improvement: | set |
Test on PR does not currently pass (possibly due to upstream changes since the PR was submitted).
comment:5 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:6 by , 10 years ago
The fix has only been added to the 1.8 branch. In Django 1.7 the behavior is still not the same as stated in the documentation.
https://docs.djangoproject.com/en/1.7/topics/testing/tools/#persistent-state
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
ifcontrib.sessions
is not installed) or if it's a documentation issue (in which case it should be mentionned that the given code only works withcontrib.sessions
installed).In any case, I'm marking this as
accepted
.Thanks for the report.
[1] https://github.com/django/django/blob/ed516a5fc8061615ec94bee37425d4fee8ca872b/django/test/client.py#L373-L382