Skip to content
Advertisement

Python Unittest Allow Multiple Possible Return Values

I’m writing a unittest that will be run on my class’s code. For one of the functions they must write, there are two possible return values that they could return and either one is okay for my purposes.

I’ve been using

actual = my_function_call(arg1, arg2)
self.assertEqual(actual, expected)

But this doesn’t work for accepting one of two valid return values, so I’ve changed it to:

actual = my_function_call(arg1, arg2)
self.assertEqual(actual == expected1 or actual == expected2, True)

Is there a way to do this that isn’t as hacky?

Advertisement

Answer

There exists assertIn test, which you could use in this case as follows

self.assertIn(actual,[expected1,expected2])

it does check if actual is present in list holding expected1,expected2.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement