Assertions
Method |
Checks that |
New in |
---|---|---|
|
used by |
2.7 |
|
all the key/value pairs in
|
2.7 |
|
|
|
|
|
|
|
|
2.7 |
|
2.7 |
|
|
|
2.7 |
|
|
2.7 |
|
|
2.7 |
|
|
2.7 |
|
|
2.7 |
|
|
2.7 |
|
|
2.7 |
|
|
2.7 |
|
2.7 |
|
|
|
|
|
|
2.7 |
|
|
2.7 |
|
|
|
|
2.7 |
|
|
|
|
|
All the assert methods (except assertRaises
and assertRaisesRegexp
accept a msg argument that, if specified, is used as the error message on
failure e.g. assertEqual(first, second, msg=None)
.
Sample
assertIn
self.assertIn('PJK', 'ABC, PJK, XYZ')
assertItemsEqual
Test that sequence expected contains the same elements as actual, regardless of their order:
self.assertItemsEqual(
names,
field_names,
'Item has invalid fields.'
)
assertRaises
from django.core.exceptions import ValidationError
with self.assertRaises(ValidationError):
contact.full_clean()
or:
from django.core.exceptions import ValidationError
from django.test import TestCase
from blog.models import Article
class ModelValidationTest(TestCase):
def test_default(self):
article = Article()
self.assertRaises(ValidationError, article.full_clean)
Note: When using assertRaises
, the method expects a callable
i.e.
don’t call the full_clean()
function, pass it without the brackets.
fail
try:
self.create_simple(request=0)
self.fail('Should throw a ValidationError.')
except ValidationError:
pass