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 "