django.test.Client
does have a login
method for this kind of thing, but given a response
object how do I determine who is logged in?
data = {'email':'[email protected]','password':'abc'}
c = Client()
# here was can assume `request.user` is the AnonymousUser
# or I can use `c.login(..)` to log someone in
r = c.post('/myform/', data)
Can my unittest determine who the request.user
would now be if I were to submit a second request?
Open /catalog/tests/test_models.py.TestCase , as shown: from django. test import TestCase # Create your tests here. Often you will add a test class for each model/view/form you want to test, with individual methods for testing specific functionality.
Just trick it to always skip with the argument True : @skipIf(True, "I don't want to run this test yet") def test_something(): ... If you are looking to simply not run certain test files, the best way is probably to use fab or other tool and run particular tests.
Writing testsDjango's unit tests use a Python standard library module: unittest . This module defines tests using a class-based approach.
The request factory class RequestFactory. The RequestFactory shares the same API as the test client. However, instead of behaving like a browser, the RequestFactory provides a way to generate a request instance that can be used as the first argument to any view.
You can do this:
client = Client()
# here was can assume `request.user` is the AnonymousUser
# or I can use `c.login(..)` to log someone in
from django.contrib import auth
user = auth.get_user(client) # it returns User or AnonymousUser
if user.is_anonymous():
...
It works because client keeps a user session (client.session).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With