我删除了用户,并更改了数据= {}部分和问题 仍然存在,我不明白是什么问题。
这是其他应用测试:
$ref
这是我测试的结果:
from django.contrib.auth.models import User
from django.urls import resolve,reverse
from django.test import TestCase
from .views import signup
from .forms import SignUpForm
class SignUpTests(TestCase):
def setUp(self):
url = reverse('signup')
self.response = self.client.get(url)
def test_signup_status_code(self):
self.assertEquals(self.response.status_code,200)
def test_signup_url_resolves_signup_view(self):
view = resolve('/signup/')
self.assertEquals(view.func,signup)
def test_csrf(self):
self.assertContains(self.response,'csrfmiddlewaretoken')
def test_contains_form(self):
form = self.response.context.get('form')
self.assertIsInstance(form,SignUpForm)
def test_form_inputs(self):
'''
The view must contain five inputs: csrf,username,email,password1,password2
'''
self.assertContains(self.response,'<input',5)
self.assertContains(self.response,'type="text"',1)
self.assertContains(self.response,'type="email"','type="password"',2)
class SuccessfulSignUpTests(TestCase):
def setUp(self):
url = reverse('signup')
data = {
'username': 'johndoe','email': 'johndoe@gmail.com','password1': 'user123','password2': 'user123'
}
self.response = self.client.post(url,data)
self.home_url = reverse('home')
def test_redirection(self):
'''
A valid form submission should redirect the user to the home page
'''
self.assertRedirects(self.response,self.home_url)
def test_user_creation(self):
self.assertTrue(User.objects.exists())
def test_user_authentication(self):
'''
Create a new request to an arbitrary page.
The resulting response should now have a `user` to its context,after a successful sign up.
'''
response = self.client.get(self.home_url)
user = response.context.get('user')
self.assertTrue(user.is_authenticated)
class InvalidSignUpTests(TestCase):
def setUp(self):
url = reverse('signup')
self.response = self.client.post(url,{}) # submit an empty dictionary
def test_signup_status_code(self):
'''
An invalid form submission should return to the same page
'''
self.assertEquals(self.response.status_code,200)
def test_form_errors(self):
form = self.response.context.get('form')
self.assertTrue(form.errors)
def test_dont_create_user(self):
self.assertFalse(User.objects.exists())
问题是我将其应用到平台上来解决这样的问题 有人从hithub复制了我的文件,您可以尝试对我的项目进行测试,并在board / accounts / test.py中记住该问题 他进行了测试,但一开始却失败了,但他说他只是更改了数据部分, 他在SuccessSignUpTests类中将数据部分更改为:
$ python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
........FFF................
======================================================================
FAIL: test_redirection (accounts.tests.SuccessfulSignUpTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "F:\MyDevelopment\boards-project\myproject\accounts\tests.py",line 55,in
test_redirection
self.assertRedirects(self.response,self.home_url)
File "F:\MyDevelopment\boards-project\venv\lib\site-packages\django\test\testca
ses.py",line 345,in assertRedirects
self.assertEqual(
AssertionError: 200 != 302 : Response didn't redirect as expected: Response code
was 200 (expected 302)
======================================================================
FAIL: test_user_authentication (accounts.tests.SuccessfulSignUpTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "F:\MyDevelopment\boards-project\myproject\accounts\tests.py",line 68,in
test_user_authentication
self.assertTrue(user.is_authenticated)
AssertionError: False is not true
======================================================================
FAIL: test_user_creation (accounts.tests.SuccessfulSignUpTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "F:\MyDevelopment\boards-project\myproject\accounts\tests.py",line 58,in
test_user_creation
self.assertTrue(User.objects.exists())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 27 tests in 0.747s
FAILED (failures=3)
Destroying test database for alias 'default'...
,如果有人想看看我在Github的存储库 这是链接: my project