As far as I know, using unittest
in Python is done something like this -
# test.py
import unittest
import math
from my_math import do_math
class test_do(unittest.TestCase):
def test_first_case(self):
self.assertEqual(math.sqrt(4), do_math.sqroot(4))
self.assertEqual(math.sqrt(9), do_math.sqroot(9))
if __name__ == "__main__":
unittest.main()
My concern is specifically with the computation we need to perform to verify the test results. If we use the setUp()
method, we could restructure the code as follows
# test.py
import unittest
import math
from my_math import do_math
class test_do(unittest.TestCase):
def setUp(self):
self.test_1 = 4
self.test_2 = 9
self.test_sqroot_1 = math.sqrt(self.test_1)
self.test_sqroot_2 = math.sqrt(self.test_2)
def test_first_case(self):
self.assertEqual(self.test_sqroot_1, do_math.sqroot(self.test_1))
self.assertEqual(self.test_sqroot_2, do_math.sqroot(self.test_2))
if __name__ == "__main__":
unittest.main()
If I need to refactor the testcases in terms of true or target values, I will need to change just the setUp() function body, whereas I would need to make changes everywhere in the first approach (considering multiple testcases accessing the same data).
My question is this - in terms of parallelized testing, given that I am doing most of the computation needed for verification in the setUp()
function and not in the testcase functions themselves, is the second approach adviseable?