What is the difference between `assertIn` and `assertContains` in Django? What is the difference between `assertIn` and `assertContains` in Django? django django

What is the difference between `assertIn` and `assertContains` in Django?


assertIn is a member of python's built-in test suit. It is a normal test for membership. For example, you can check the membership of an element of an array. A key in a dictionary etc. You can use it for everything the in operator can be used for.

assertContains is added by Django in its test suit. It is used specifically for responses. That means you can pass it response object returned by the view. It will evaluate it and check for membership then.

You can read about assertIn and assertContains in respective documentations.


Django testing inherits from unittest in Python.

assertContains is specific to Django and allows you to evaluate additional things beyond a simple assertIn:

SimpleTestCase.assertContains(response, text, count=None, status_code=200, msg_prefix='', html=False)[source]

Asserts that a Response instance produced the given status_code and that text appears in the content of the response. If count is provided, text must occur exactly count times in the response.

While assertIn does simple evaluation:

assertIn(first, second, msg=None)

assertIn(a, b) checks for a in b