Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Test Time Template Tests

Lewis Cowles
Lewis Cowles
74,902 Points

Can `assertContains` produce false positives?

random question, but previously we used assertIn to find is something is in something else. With the response we specifically passed the context so that we could verify data being sent to the view / that the correct article was retrieved.

So my question is does assertContains have checks to prevent false positives when checking for X containing Y. What would happen if something is sent to the view in context, that was not output?

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

assertContains only checks against the text attribute of a Response object (HttpResponse and friends) so it doesn't care what's in the context because it doesn't know about the context at all.

But you do occasionally get false positives when you search for something that's easily contained in the output but isn't the thing you're looking for. Consider checking for a number of items, like 3, when your HTML contains an h3 tag.