Search code examples
pythonunit-testingjunit-theory

Unit test Theories in Python?


In a previous life I did a fair bit of Java development, and found JUnit Theories to be quite useful. Is there any similar mechanism for Python?

Currently I'm doing something like:

def some_test(self):
    cases = [('some sample input', 'some expected value'),
            ('some other input', 'some other expected value')]

    for value, expected in cases:
        result = method_under_test(value)
        self.assertEqual(expected, result)

But this is rather clunky, if the first "case" fails, all others fail to be run.


Solution

  • It looks like pytest can do something like this: https://docs.pytest.org/en/latest/parametrize.html

    I haven't tried it out myself.