title: Django Unit Testing - Client login does not seem to work
body: <p>I have been referring to the tutorial in <a href="https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Testing" rel="nofollow noreferrer">https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Testing</a> when working on my own unit testing. My unit test codes are below:  </p>
  
  <pre><code>from django.core.urlresolvers import reverse
  from django.test import Client, TestCase
  from django.contrib.auth.models import User
  
  from companies.models import Company
  
  class ContactViewTest(TestCase):
  
      def setUp(self):
          self.client = Client(HTTP_HOST='localhost:8000')
  
          company = Company(name='Test')
          company.save()
  
          user = User.objects.create(username='test_user1', email='test_user1@test.com')
          user.set_password('password')
          user.save()
  
          self.user = user
          self.company = company
  
      def test_logged_in_all_contacts_correct_template(self):
          c = self.client
          user_login = c.login(username=self.user.username, password=self.user.password)
          self.assertTrue(user_login)
  
          resp = c.get(reverse('all-contacts'), follow=True)
  
          # Check if user is logged in
          self.assertEquals(str(resp.context['user']), 'test_user1')
          # Check if response is "success"
          self.assertEqual(resp.status_code, 200)
  
          self.assertTemplateUsed(resp, 'all_contacts.html')
  </code></pre>
  
  <p>Upon running python manage.py test contacts.tests, I ran into a few errors.</p>
  
  <p>The first being <code>self.assertTrue(user_login)</code> which returns <code>AssertionError: False is not true</code></p>
  
  <p>The second being <code>self.assertEquals(str(resp.context['user']), 'test_user1')</code> which returns <code>AssertionError: 'AnonymousUser' != 'test_user1'</code> when I removed <code>self.assertTrue(user_login)</code></p>
  
  <p>I am not really sure what went wrong other than the client.login() did not work as I expected so thanks in advance to whoever can help.</p>