mercredi 13 novembre 2019

AssertionError: False is not true, the tests failed every time because that reason?

i deleted the user and change the data = {} part and the issue still exist and i don't understand what is the problem .

This is the other app tests :

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())

and this is the the results of my tests:

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

and the problem is i applied that on platform for fixing problems like this and someone copied my files from hithub and you can try to run test on my project and remember the issue in boards/accounts/test.py and he ran the tests and it fails at the beginning but he said he changed just the data part, he changed in SuccessfulSignUpTests class the data part to :

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

and if anyone want to take a look at my repository at Github this is the link : my project

Aucun commentaire:

Enregistrer un commentaire