小编Aml*_*oti的帖子

Django 单元测试在登录时提供匿名用户

我有以下用于 Django 登录单元测试的代码。

"""This is the unit test case for form element"""
from django.test import TestCase
from django.contrib.auth.models import User


class TestForm(TestCase):
    def setUp(self):
        self.credentials = {
            'username': 'abhishek',
            'password': 'Abhishek@12345'}
        User.objects.create_user(**self.credentials)

    def test_login(self):
        # send login data
        response = self.client.post('/accounts/login', self.credentials, follow=True)
        # should be logged in now
        print(response.context['user'])
        self.assertTrue(response.context['user'].is_authenticated)
Run Code Online (Sandbox Code Playgroud)

当我通过我的命令 python ../manage.py test accounts.tests.test_form.TestForm 执行这个单元测试时,它给出了以下错误。

Creating test database for alias 'default'...
System check identified no issues (0 silenced).
AnonymousUser
F
======================================================================
FAIL: test_login (accounts.tests.test_form.TestForm)
----------------------------------------------------------------------
Traceback (most recent …
Run Code Online (Sandbox Code Playgroud)

python django unit-testing testcase

5
推荐指数
1
解决办法
697
查看次数

标签 统计

django ×1

python ×1

testcase ×1

unit-testing ×1