I'm very new in django, and currently was following a tutorial and I was trying to test users are listed on user page.
(我在django中是非常陌生的人,目前正在关注一个教程,并且我正尝试测试用户。)
So I setup test_admin.py. (所以我设置了test_admin.py。)
And i get an AssertionError 302 != 200. (我得到一个AssertionError 302!= 200。)
Below are my test_admin.py
(以下是我的test_admin.py)
from django.test import TestCase,Client
from django.contrib.auth import get_user_model
from django.urls import reverse
class AdminSiteTests(TestCase):
def setUp(self):
self.client = Client()
self.admin_user = get_user_model().objects.create_superuser(
email='admin@gmail.com',
password='092100027h'
)
self.client.force_login(self.admin_user)
self.user = get_user_model().objects.create_user(
email='test@gmail.com',
password='092100027h',
name='Test User Full Name'
)
def test_users_listed(self):
"""
Test that users are listed on user page
"""
url = reverse('admin:core_user_changelist')
res = self.client.get(url)
self.assertContains(res, self.user.name)
self.assertContains(res, self.user.email)
and this is my admin.py
(这是我的admin.py)
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from . import models
class UserAdmin(BaseUserAdmin):
ordering = ['id']
list_display = ['email', 'name']
admin.site.register(models.User, UserAdmin)
and this is my error
(这是我的错误)
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F....
======================================================================
FAIL: test_users_listed (core.tests.test_admin.AdminSiteTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/app/core/tests/test_admin.py", line 28, in test_users_listed
self.assertContains(res, self.user.name)
File "/usr/local/lib/python3.7/site-packages/django/test/testcases.py", line 446, in assertContains
response, text, status_code, msg_prefix, html)
File "/usr/local/lib/python3.7/site-packages/django/test/testcases.py", line 418, in _assert_contains
" (expected %d)" % (response.status_code, status_code)
AssertionError: 302 != 200 : Couldn't retrieve content: Response code was 302 (expected 200)
----------------------------------------------------------------------
Ran 5 tests in 0.795s
FAILED (failures=1)
Destroying test database for alias 'default'...
ask by metalhaedcoder translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…