If I have tests for simple functions, fun1 and fun2, which need some arguments:
class TestOne(unittest.TestCase):
def test_func1(self):
a = 0
b = 1
c = 2
self.assertEquals(c, fun1(a,b))
def test_fun2(self):
d = 0
e = 1
f = 2
self.assertEquals(f, fun2(d,e))
and a test for a third function, which need the output of fun1 and fun2 as input
class TestTwo(unittest.TestCase):
def test_fun3(self):
a = 0
b = 1
d = 0
e = 1
g = 3
self.assertEquals(g, fun3(fun1(a,b), fun2(d,e)))
what is the best way to avoid having to re-write the arguments of the first functions?
fun1andfun2in your unit test forfun3? Why not just put the expected results of fun1 and fun2 into the test for fun3? In other words, iffun1(0, 1)returns 5 andfun2(0, 1)returns 6, write your test forfun3asfun3(5, 6).fun1andfun2and in some case the difference of value is in the last decimals, but I have the same question, is a good idea use functions as input in a unit test ?fun1orfun2change logic, your tests forfun3may now fail (even though fun3 is still correct). Likewise, a test forfun3might pass even thoughfun3is implemented incorrectly becausefun2is actually implemented wrong. Your tests might also run slower, and be more likely to fail. If your issue is decimal places, you should learn to set up a variable with the problematic decimals. In addition to making your tests less reliant on other functions, it makes the test more clear as to what you're testing.