My code:
import unittest
class MyTestCase(unittest.TestCase):
def setUp(self):
print("setUp")
def tearDown(self):
print("tearDown")
def test_something(self):
for i in range(4):
with self.subTest():
self.assertEqual(True, True) # add assertion here
if __name__ == '__main__':
unittest.main()
Run the test test_something
in it, and we get the result:
test_something (tests.ui.test_example.MyTestCase) ... setUp
tearDown
ok
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
You can see I use self.subTest()
to parameterize my test, which is convenient. The problem is, I want python unittest to call setUp
and tearDown
for each subTest
, i.e. they should be called 4 times each. However, they are actually only called once each (You can verify that from the test output above). Is there any way to achieve it?