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)
asked Jul 2, 2021 at 14:44
Jim Jeffries
10.1k16 gold badges66 silver badges105 bronze badges
1 Answer 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)
answered Jul 5, 2021 at 20:10
D Malan
11.7k3 gold badges35 silver badges65 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
lang-py
unittestitself doesn't provide something like this, butpytestdoes, and I've used theddtmodule in the past withunittest.ddtprovides exactly what I'm looking for. If you add that as an answer I'll accept it.