How do I run multiple Python test cases in a loop?

前端 未结 4 1599
忘了有多久
忘了有多久 2021-02-03 21:50

I am new to Python and trying to do something I do often in Ruby. Namely, iterating over a set of indices, using them as argument to function and comparing its results with an a

4条回答
  •  梦谈多话
    2021-02-03 22:17

    Starting from python 3.4, you can do it like this:

    def test_output(self):
        for i in range(1,11):
            with self.subTest(i=i):
                ....
                self.assertEqual(fn(i),output[i])
    

    https://docs.python.org/3.4/library/unittest.html?highlight=subtest#distinguishing-test-iterations-using-subtests

提交回复
热议问题