[Django] #35840: assertFormError shows error when testing form input

12 views
Skip to first unread message

Django

unread,
Oct 15, 2024, 2:56:02 AM10/15/24
to django-...@googlegroups.com
#35840: assertFormError shows error when testing form input
-------------------------------------+-------------------------------------
Reporter: KevinS | Type: Bug
Status: new | Component: Testing
| framework
Version: 5.1 | Severity: Normal
Keywords: UnitTest, Form | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
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`
{{{#!python
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`

{{{#!python
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`

{{{#!python
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)

....
}}}



{{{#!zsh
======================================================================
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'

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

}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35840>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 15, 2024, 4:40:37 AM10/15/24
to django-...@googlegroups.com
#35840: assertFormError shows error when testing form input
-----------------------------------+--------------------------------------
Reporter: KevinS | Owner: (none)
Type: Bug | Status: closed
Component: Testing framework | Version: 5.1
Severity: Normal | Resolution: invalid
Keywords: UnitTest, Form | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* resolution: => invalid
* status: new => closed

Comment:

`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
--
Ticket URL: <https://code.djangoproject.com/ticket/35840#comment:1>

Django

unread,
Oct 15, 2024, 5:12:48 AM10/15/24
to django-...@googlegroups.com
#35840: assertFormError shows error when testing form input
-----------------------------------+--------------------------------------
Reporter: KevinS | Owner: (none)
Type: Bug | Status: closed
Component: Testing framework | Version: 5.1
Severity: Normal | Resolution: invalid
Keywords: UnitTest, Form | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by KevinS):

* Attachment "ScreenShot of AssetFormError Method.jpg" added.

Django

unread,
Oct 15, 2024, 5:19:24 AM10/15/24
to django-...@googlegroups.com
#35840: assertFormError shows error when testing form input
-----------------------------------+--------------------------------------
Reporter: KevinS | Owner: (none)
Type: Bug | Status: new
Component: Testing framework | Version: 5.1
Severity: Normal | Resolution:
Keywords: UnitTest, Form | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by KevinS):

* resolution: invalid =>
* status: closed => new

Comment:

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.
--
Ticket URL: <https://code.djangoproject.com/ticket/35840#comment:2>

Django

unread,
Oct 15, 2024, 7:18:47 AM10/15/24
to django-...@googlegroups.com
#35840: assertFormError shows error when testing form input
-----------------------------------+--------------------------------------
Reporter: KevinS | Owner: (none)
Type: Bug | Status: closed
Component: Testing framework | Version: 5.1
Severity: Normal | Resolution: invalid
Keywords: UnitTest, Form | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Sarah Boyce):

* resolution: => invalid
* status: new => closed

Comment:

That looks like the IDE has gotten confused and is picking up something
else
Doesn't look to be a bug in Django
--
Ticket URL: <https://code.djangoproject.com/ticket/35840#comment:3>

Django

unread,
Oct 16, 2024, 2:39:07 AM10/16/24
to django-...@googlegroups.com
#35840: assertFormError shows error when testing form input
-----------------------------------+--------------------------------------
Reporter: KevinS | Owner: (none)
Type: Bug | Status: closed
Component: Testing framework | Version: 5.1
Severity: Normal | Resolution: fixed
Keywords: UnitTest, Form | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by KevinS):

* resolution: invalid => fixed

Comment:

Hi Team,

Thank you. I tested again, ignoring the prompt from the IDE. It works!
--
Ticket URL: <https://code.djangoproject.com/ticket/35840#comment:4>

Django

unread,
Oct 16, 2024, 9:42:46 AM10/16/24
to django-...@googlegroups.com
#35840: assertFormError shows error when testing form input
-----------------------------------+--------------------------------------
Reporter: KevinS | Owner: (none)
Type: Bug | Status: closed
Component: Testing framework | Version: 5.1
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Natalia Bidart):

* keywords: UnitTest, Form =>
* resolution: fixed => invalid

Comment:

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).
--
Ticket URL: <https://code.djangoproject.com/ticket/35840#comment:5>
Reply all
Reply to author
Forward
0 new messages