One of
the more rigorous ways to test if your code does its job is using the built in
unittest module. In order to test code rigorously, it is important to pick good
test cases. Things to consider when choosing test cases is interesting cases,
size, mixture, opposites, and order. Many of the things listed are not
applicable to every function, and some functions require additional tests not
mentioned here. The tests used completely depend on the function you are trying
to test.
Interesting
Cases
Interesting
cases are legal inputs that you think might break your code.
Size
If your
function has a collection of objects (list, dict) as one of its parameters,
sometimes it is useful to test by varying the number of objects because the
function may work for only a range of sizes.
Mixture
Mixing
the values in the collections is also sometimes useful, especially when your
function tries to isolate certain values.
Opposites
If your
functions plays around with opposites (positive or negative, left or right,
True or False) testing each one out is essential because they are boundary
cases.
Order
Playing
around with the order (sorted, unsorted) can also break certain functions that
don’t know how to handle objects when given in random order.
For a
function that has a string parameter, some general tests could be
“” (Interesting case, size of 0)
“” (Interesting case, size of 0)
“a” (size
of 1)
“aaa” (size
of 3, same character, order)
“ab c=3”
(size of 6, mixture, order)
Notice
that multiple things were tested in each case to minimize the amount of tests
needed. Can you think of any additional tests that could be useful?