【问题标题】:AssertionError: False is not true, the tests failed every time because that reason?AssertionError: False 不正确,测试每次都失败是因为那个原因?
【发布时间】:2020-03-09 11:00:06
【问题描述】:

我删除了用户并更改了 data = {} 部分和问题 仍然存在,我不明白是什么问题。

这是其他应用测试:

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"', 1)
    self.assertContains(self.response, '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())

这是我的测试结果:

$ 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'...

问题是我在平台上应用了它来解决这样的问题 有人从 hithub 复制了我的文件,您可以尝试在我的项目上运行测试并记住 board/accounts/test.py 中的问题 他进行了测试,一开始就失败了,但他说他只改变了数据部分, 他在 SuccessSignUpTests 类中将数据部分更改为:

data = {
   'username': 'johndoe',
   'email': 'johndoe@gmail.com',
   'password1': 'user123'
   'password2': 'user123'
}

如果有人想看看我在 Github 上的存储库 这是链接: my project

【问题讨论】:

    标签: python django unit-testing testing


    【解决方案1】:

    我猜问题出在你的view.py

    def signup(request):
        if request.method == 'POST':
            form = SignUpForm(request.POST)
            if form.is_valid():
                user = form.save()
                auth_login(request, user)
                return redirect('home')
    

    首先,你不应该直接使用 url 名称,以防你以后决定重构 url。但是,如果您使用,则 url 应该是相对的,例如 /home

    最好使用reverse。所以用

    替换 return redirect('home')
    return redirect(reverse('home'))
    

    您必须添加以下导入

    from django.urls import reverse
    

    【讨论】:

    • 不,解决方案是我应该更改数据部分,setUp 函数中的实际数据并更改虚幻数据,我猜密码或单词的数字或密码有问题'不验证,但不幸的是最后它是视图部分。
    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    • 2018-06-03
    相关资源
    最近更新 更多