2

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) 
3
  • unittest itself doesn't provide something like this, but pytest does, and I've used the ddt module in the past with unittest. Commented Jul 2, 2021 at 14:51
  • @chepnerddt provides exactly what I'm looking for. If you add that as an answer I'll accept it. Commented Jul 2, 2021 at 15:18
  • 1
    Does this answer your question? How do you generate dynamic (parameterized) unit tests in Python? Commented Jul 10, 2021 at 8:29

1 Answer 1

2

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) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.