Jim Jeffries
Jim Jeffries

Reputation: 10081

Is there a way of running the same python test with different inputs

Is there a way of running the same python test with multiple inputs? Ideally using unittest, but open to other libraries if not.

I'd like to be able to write something along the lines of

@inputs(1,2,3)
@inputs(4,5,9)
def test_my_test(self, a, b, c):
    self.assertEqual(a + b, c)

Upvotes: 1

Views: 714

Answers (1)

D Malan
D Malan

Reputation: 11464

Since Python 3.4, the subTest context manager provides a similar functionality:

import unittest


class MyTest(unittest.TestCase):
    def test_my_test(self):
        inputs = [
            (1,2,3),
            (4,5,9),
        ]
        for a, b, c in inputs:
            with self.subTest(a=a, b=b, c=c):
                self.assertEqual(a + b, c)

Upvotes: 1

Related Questions