Python/Django: how to assert that unit test result contains a certain string?
self.assertContains(result, "abcd")
You can modify it to work with json.
Use self.assertContains
only for HttpResponse
objects. For other objects, use self.assertIn
.
To assert if a string is or is not a substring of another, you should use assertIn
and assertNotIn
:
# Passes
self.assertIn('bcd', 'abcde')
# AssertionError: 'bcd' unexpectedly found in 'abcde'
self.assertNotIn('bcd', 'abcde')
These are new since Python 2.7 and Python 3.1
You can write assertion about expected part of string in another string with a simple assertTrue + in python keyword :
self.assertTrue("expected_part_of_string" in my_longer_string)