Testing Django User Login Form

147 views
Skip to first unread message

lone...@gmail.com

unread,
Apr 18, 2021, 6:40:55 PM4/18/21
to Django users
Hello all,

    I am not sure what I am doing incorrectly with the below test case information.  I am simply trying to test the default authentication form of django 3.1.  This test should be a good/True login test.

Here is the Test case:-----------------
class TestForms(TestCase):
    def test_invalid_username(self):
        # The user submits an invalid username.
        data = {
            'username': 'abc',
            'password': 'password',
        }
        form = AuthenticationForm(None, data)
        self.assertTrue(form.is_valid())
----------------------------

Here is what I am getting for output:
--------------------
FAIL: test_invalid_username (rucklogs.tests.TestForms)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/lonesoac0/django/rucker2021/rucklogs/tests.py", line 39, in test_invalid_username
    self.assertTrue(form.is_valid())
AssertionError: False is not true
-------------------

As you can see in the attached screenshot, the user abc is a good user.

Thank you.

good_user.PNG

David Nugent

unread,
Apr 22, 2021, 7:31:40 PM4/22/21
to django...@googlegroups.com
Try creating a request using RequestFactory and use that to input the credentials. I don't think the way you are creating the form is valid.

rf = RequestFactory()
request = rf.post('/some_mock_url', dict(username='abc', password='password'))
form = AuthenticationForm(request)

lone...@gmail.com

unread,
Apr 23, 2021, 7:32:03 AM4/23/21
to Django users
Thank you for the reply!  I will totally try this tonight!
Reply all
Reply to author
Forward
0 new messages