'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?



Solution 1:[1]

You can do assertTrue and in

self.assertTrue(actual in (expected1, expected2))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jonrsharpe