From f7e0bffa2e9c8820c008e742f76b71feaf7dd89f Mon Sep 17 00:00:00 2001 From: Baptiste Mispelon Date: Tue, 15 Feb 2022 10:28:26 +0100 Subject: [PATCH] Refs #33348 -- Made SimpleTestCase.assertFormError() raise ValueError when "field" is passed without "form_index". --- django/test/testcases.py | 4 ++++ tests/test_utils/tests.py | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/django/test/testcases.py b/django/test/testcases.py index 5533c01db4..3758671117 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -658,6 +658,10 @@ class SimpleTestCase(unittest.TestCase): stacklevel=2, ) errors = [] + + if form_index is None and field is not None: + raise ValueError("You must use field=None with form_index=None.") + # Put error(s) into a list to simplify processing. errors = to_list(errors) diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index 92778439ed..f62a473357 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -1603,6 +1603,17 @@ class AssertFormsetErrorTests(SimpleTestCase): ) self.assertFormsetError(response, "formset", None, None, "error") + def test_non_form_errors_with_field(self): + response = mock.Mock( + context=[ + {}, + {"formset": TestFormset.invalid(nonform=True)}, + ] + ) + msg = "You must use field=None with form_index=None." + with self.assertRaisesMessage(ValueError, msg): + self.assertFormsetError(response, "formset", None, "field", "error") + def test_form_index_too_big(self): msg = ( "The formset only has "