mardi 26 septembre 2017

How to write a unit test to ensure output is within certain limits?

If I have a requirement that says the output of a function must always be within certain limits, how can I test this if the function returns unpredictable results?

For example, lets say I have a function that generates random numbers or passwords; how can I test that the number is always between 1000 and 9999 or the password is always between 6-12 characters?

Let's use the number generator example - currently I would write two tests: one that runs the function in a finite loop and tests each result is >= 1000 and a second that ensures the output is <= 9999. Obviously the loop must be run a large number of times (>9K) for each test to have any chance of covering all the possible output values.

Are there any better approaches (i.e. ones that would be considered more reliable) when testing the output of unpredictable functions




Aucun commentaire:

Enregistrer un commentaire