I've enabled login authentication for my django app. Unit tests for views are not working because they get stuck at the login page. Setting a breakpoint immediately after the view's response is returned and using
print response.content
results in output that contains the phrase 'please login'
How do I setup a user so the testcase can login? I've tried using my username and password, but it doesn't work in the unit test context.
Django by default will look within a templates folder called registration for auth templates. The login template is called login. html . Create a new directory called templates and within it another directory called registration .
The preferred way to write tests in Django is using the unittest module built-in to the Python standard library. This is covered in detail in the Writing and running tests document. You can also use any other Python test framework; Django provides an API and tools for that kind of integration.
To write a test you derive from any of the Django (or unittest) test base classes (SimpleTestCase, TransactionTestCase, TestCase, LiveServerTestCase) and then write separate methods to check that specific functionality works as expected (tests use "assert" methods to test that expressions result in True or False values ...
The following code inserted at the beginning of the testcase creates a user, logs them in, and allows the rest of the test to contiue
self.user = User.objects.create_user(username='testuser', password='12345') login = self.client.login(username='testuser', password='12345')
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