lundi 19 septembre 2016

Unit testing very simple functions

Say I have a simple function of the form:

def square(x):
    return x**2

If I write a unit test for testing correctness, is it considered bad practice to do something like:

def test_square(self):
        for _ in range(50):
            rand_num = random.uniform(-10,10)
            self.assertAlmostEqual(square(rand_num), x**2, msg= "Failed for input: {}".format(rand_num))

Where essentially instead of writing manual cases, I'm in a sense rewriting the function inside the unit test? Why or why won't this be considered good practice.

PS: I'm assuming there are other tests which check for invalid inputs and stuff, I'm asking this for the very specific case of testing correctness of the function.

Aucun commentaire:

Enregistrer un commentaire