Search code examples
pythonjsondjangounit-testingassert

Python/Django: how to assert that unit test result contains a certain string?


In a python unit test (actually Django), what is the correct assert statement that will tell me if my test result contains a string of my choosing?

self.assertContainsTheString(result, {"car" : ["toyota","honda"]})

I want to make sure that my result contains at least the json object (or string) that I specified as the second argument above

{"car" : ["toyota","honda"]}

Solution

  • 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.