Opened 4 weeks ago

Closed 4 weeks ago

Last modified 4 weeks ago

#35840 closed Bug (invalid)

assertFormError shows error when testing form input

Reported by: KevinS Owned by:
Component: Testing framework Version: 5.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi all,

I was encountering some issue using the self.assertFormError to test a simple form input.
Not sure why it keeps giving me the following error; I thought once the form is posted, it should be bound.

forms.py

class Form_Test(forms.Form):
    name = forms.CharField(max_length=20)
    integer = forms.IntegerField()
    
    def clean_integer(self):
        c = self.cleaned_data['integer']
        if c < 10:
            self.add_error(
                field='integer',
                error='Must be bigger than 10'
            )
        else:
            return c

views.py

def FormTest(request):
    if request.method == 'POST':
        form = Form_Test(request.POST)
        if form.is_valid():
            print('Hello World')
    else:
        form = Form_Test()
    return render(request, 'basic_form.html', context={'form': form})

urls.py ==> path('test-form/', views.FormTest, name='test_form'),

test_views.py

from django.test import TestCase, 
from django.contrib.auth.models import User
from django.urls import reverse
from ChurchAccount.forms import Form_Test

class AuthViewsTest(TestCase):
    def setUp(self):
        self.form_url = reverse('test_form')

    def test_form_test(self):
        response = self.client.post(self.form_url, data={'name': 'abc','integer': 2})
        self.assertFormError(response, 'form', 'integer', 'Must be bigger than 10')
        self.assertEqual(response.status_code, 200)

    ....
======================================================================
ERROR: test_form_test (ChurchAccount.tests.test_views.AuthViewsTest.test_form_test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Development/Church/ChurchSite5/ChurchAccount/tests/test_views.py", line 107, in test_form_test
    self.assertFormError(response, 'form', 'integer', 'Must be bigger than 10')
  File "/.pyenv/versions/3.12.7/envs/web-dev/lib/python3.12/site-packages/django/test/testcases.py", line 698, in assertFormError
    self._assert_form_error(form, field, errors, msg_prefix, f"form {form!r}")
  File "/.pyenv/versions/3.12.7/envs/web-dev/lib/python3.12/site-packages/django/test/testcases.py", line 664, in _assert_form_error
    if not form.is_bound:
           ^^^^^^^^^^^^^
AttributeError: 'HttpResponse' object has no attribute 'is_bound'

----------------------------------------------------------------------

Attachments (1)

ScreenShot of AssetFormError Method.jpg (204.8 KB ) - added by KevinS 4 weeks ago.

Download all attachments as: .zip

Change History (6)

comment:1 by Mariusz Felisiak, 4 weeks ago

Resolution: invalid
Status: newclosed

assertFormError() accepts form as the first argument, not a response. Please don't use Trac for support questions as it's not a support channel.

TicketClosingReasons/UseSupportChannels

comment:2 by KevinS, 4 weeks ago

Resolution: invalid
Status: closednew

Hi,

Understand that this is not a support ticket and I have already search other forums for a solution. But given your comment and I have check Django documentation, (https://docs.djangoproject.com/en/5.1/topics/testing/tools/#django.test.SimpleTestCase.assertFormError) and that the parameters are indeed form as first parameter.

That why I am confused as my screenshot shows that the first parameter is a HttpResponse. Hence I have raise it here as a potential bug. But do direct me to the appropriate channel if it is still not an issue to be raised here.

comment:3 by Sarah Boyce, 4 weeks ago

Resolution: invalid
Status: newclosed

That looks like the IDE has gotten confused and is picking up something else
Doesn't look to be a bug in Django

comment:4 by KevinS, 4 weeks ago

Resolution: invalidfixed

Hi Team,

Thank you. I tested again, ignoring the prompt from the IDE. It works!

comment:5 by Natalia Bidart, 4 weeks ago

Keywords: UnitTest Form removed
Resolution: fixedinvalid

Hello Kevin, I'm glad you are unblocked. But please do not change the resolution option since this is an invalid bug report (it was a support request that should have been sent to the Django User forum).

Note: See TracTickets for help on using tickets.
Back to Top