1

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)
Jim Jeffries
  • 9,841
  • 15
  • 62
  • 103
  • `unittest` itself doesn't provide something like this, but `pytest` does, and I've used the [`ddt` module](https://pypi.org/project/ddt/) in the past with `unittest`. – chepner Jul 02 '21 at 14:51
  • @chepner`ddt` provides exactly what I'm looking for. If you add that as an answer I'll accept it. – Jim Jeffries Jul 02 '21 at 15:18
  • 1
    Does this answer your question? [How do you generate dynamic (parameterized) unit tests in Python?](https://stackoverflow.com/questions/32899/how-do-you-generate-dynamic-parameterized-unit-tests-in-python) – Dirk Herrmann Jul 10 '21 at 08:29

1 Answers1

1

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)
D Malan
  • 10,272
  • 3
  • 25
  • 50