Accessing Django test client in setUpClass

78 views
Skip to first unread message

Nicole Harris

unread,
Jan 22, 2015, 4:08:48 AM1/22/15
to django...@googlegroups.com
I just posted this on Stack Overflow and then realised I might have more luck here.

Basically, I'd like to know if I can access self.client inside setUpClass(cls) when setting up a Django test:
http://stackoverflow.com/questions/28084683/accessing-django-test-client-in-setupclass

If anybody has any insight on this, I'd really appreciate it :)

Russell Keith-Magee

unread,
Jan 22, 2015, 7:09:38 PM1/22/15
to Django Users
Hi Nicole,

(I've added this answer to SO as well)

Short answer is no - but that's not the end of the story.

self.client is a convenience that is configured as part of the "pre-test" sequence. This means it is configured on a per-test basis; so you can use it in setUp(), but not setUpClass(). 

However, there's nothing especially magical about self.client - it's just an instance of django.test.Client that Django sets up for you because it's something that is very useful to have around. You can set up your own client if you want - you just have to instantiate an instance of Client:

from django.test import Client
...
self.myclient = Client()

and then use self.myclient as you would self.client. This can be very useful if you need to check "two user" behaviour - for example, checking that if an admin approves an article, a user can then see it. You create two clients, log them in with separate accounts, and then GET the same article URL with each client. In your case, you could create a client purely for class setup activities.

The only caveat on using django.test.Client in setUpClass() relate to transactional side effects. setUpClass() doesn't fall inside the transaction protection of a test case, so anything you do in setUpClass() will be "permanent" on the test database. You'll need to manually roll back or undo any database changes that setUpClass makes, or you'll get cross-testcase side effects.

If you're using Django 1.8, you can use setUpTestData() instead - in this case, anything the client does *would* be protected by a transaction.

Yours,
Russ Magee %-)


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fb4e3cd7-a70a-4a7c-b61f-f6ef73d7e226%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Fred Stluka

unread,
Jan 29, 2015, 4:52:33 PM1/29/15
to django...@googlegroups.com
Nicole,

If you can't access self.client of TestCase, you can always
allocate one yourself as:

    c = Client()

See details in:
- http://www.dougalmatthews.com/2010/Jan/20/testing-your-first-django-app/

--Fred
Fred Stluka -- mailto:fr...@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.
Reply all
Reply to author
Forward
0 new messages