Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django assert that response contains one of a list of possible strings

I'm writing tests for my Django app using the built-in testing tools. Right now I'm trying to write a test for a page that displays a list of a user's followers. When a user has no followers the page displays a message randomly picked from a list of strings. As an example:

NO_FOLLOWERS_MESSAGES = [
    "You don't have any followers.", 
    "Sargent Dan, you ain't got no followers!"
]

So now I want to write a test that asserts that the response contains one of those strings. If I was only using one string, I could just use self.assertContains(request, "You don't have any followers.") but I'm stuck on how to write the test with multiple possible outcomes. Any help would be appreciated.

like image 796
BenWurth Avatar asked Nov 17 '25 18:11

BenWurth


1 Answers

Try this:

if not any([x in response.content for x in NO_FOLLOWERS_MESSAGES]):
        raise AssertionError("Did not match any of the messages in the request")

About any(): https://docs.python.org/2/library/functions.html#any

like image 123
Mikko Ohtamaa Avatar answered Nov 20 '25 08:11

Mikko Ohtamaa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!