Skip to content

Instantly share code, notes, and snippets.

@loic
Last active December 19, 2015 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loic/6008551 to your computer and use it in GitHub Desktop.
Save loic/6008551 to your computer and use it in GitHub Desktop.
diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py
index 09c62c5..d64295c 100644
--- a/tests/model_forms/tests.py
+++ b/tests/model_forms/tests.py
@@ -1800,3 +1800,24 @@ class M2mHelpTextTest(TestCase):
html = form.as_p()
self.assertInHTML('<ul id="id_status">', html)
self.assertInHTML(dreaded_help_text, html, count=0)
+
+
+from django.forms import ModelForm
+from django.db import models
+class Person(models.Model):
+ age = models.IntegerField(
+ error_messages={'invalid': 'Custom message.'},
+ )
+
+class ContactForm(ModelForm):
+ class Meta:
+ model = Person
+ fields = ('age',)
+
+class ContactTest(TestCase):
+ def test_contact(self):
+ form = ContactForm({'age': 'abc'})
+ self.assertHTMLEqual(
+ str(form.errors['age']),
+ '<ul class="errorlist"><li>Custom message.</li></ul>',
+ )
$ PYTHONPATH=. python3 ./tests/runtests.py --settings=test_sqlite model_forms
Creating test database for alias 'default'...
Creating test database for alias 'other'...
F.............................s........................
======================================================================
FAIL: test_contact (model_forms.tests.ContactTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/loic/Dev/django/tests/model_forms/tests.py", line 1822, in test_contact
'<ul class="errorlist"><li>Custom message.</li></ul>',
File "/Users/loic/Dev/django/django/test/testcases.py", line 635, in assertHTMLEqual
self.fail(self._formatMessage(msg, standardMsg))
AssertionError: <ul class="errorlist">
<li>
Enter a whole number.
</li>
</ul> != <ul class="errorlist">
<li>
Custom message.
</li>
</ul>
<ul class="errorlist">
<li>
- Enter a whole number.
+ Custom message.
</li>
</ul>
----------------------------------------------------------------------
Ran 55 tests in 0.310s
FAILED (failures=1, skipped=1)
Destroying test database for alias 'default'...
Destroying test database for alias 'other'...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment