During Monday's lecture, we discussed the various aspects of testing such as constructing test cases and boundary conditions, while at the same time not 'over-testing'. Perhaps the most logical thing to compare testing to is a sieve. With testing, the objective is to 'catch' as many ways possible where you function could execute incorrectly or cause an error. As such, it is necessary to construct various 'gradations' or 'filters' each more refined than the last until all potential errors have been dealt with.
The challenge for me lies in the proper construction of test cases. At times, just starting an assignment, or even a problem in general, it is hard to envision where potential errors will arise in the code since functions, methods, and classes tend to all be interconnected and often make very different assumptions about what input will be and how it is to be handled. We can analyze the example used Monday to illustrate this point. Observe the following function:
def insert_after(L, n1, n2):
"""
(list, int, int) -> NoneType
After each occurrence of n1 in L, insert n2.
>>> L = [5, 1, 2, 1, 6]
>>> insert_after(L, 1, 99)
>>> L
[5, 1, 99, 2, 1, 99, 6]
"""
i = 0
while i < len(L):
if L[i] == n1:
L.insert(i+1, n2)
i += 1
i += 1
While it seems simple enough (i.e., just placing n2 after each occurrence of n1 in a list L), it can in fact pose several hidden pitfalls. For example, what if n1 and n2 are equal? If the last line of code (i += 1) outside of the while loop were absent, an infinite loop would be triggered after the first occurrence of n1. Why? Because adding n2 after n1 and indexing up 1 means the next number to compare in the list is the newly-added n2 which equals n1 so the while loop would execute ad nauseam.
As such, while many peoples' first test cases would focus on the numbers themselves (i.e., negatives, 0) or on the composition of the list (i.e., no occurrences of n1, few occurrences of n1), the real pitfall lies in the relation between the two parameters n1 and n2 (a much less intuitive test case). After having studied this example more thoroughly, I more fully appreciate the importance of developing strong test cases BEFORE starting to code away. Not only will this make individual functions like the one above 'bulletproof' but it will also prevent a potentially devastating cascade of errors further down the road when this function (or others) could be incorporated into larger problems. After all, a chain is only strong as its weakest link and analogously, a module is only as strong as its weakest function.
On a final note, the term test went well and was less difficult than what I had expected. Although the first question took more time than I expected, it was definitely 'doable' in the 50 minute time-frame allotted. Overall, going into week 6, I am both happy that reading week is fast approaching and that I have made good progress so far in the course.
Wow Jonah! You have a disguised testing fetish. Why didn't I know...
ReplyDeleteAs for code testing, the testing that you describe is called unit testing, since you are testing individual methods within a class, which are the smallest possible pieces of code that can be tested. Later on, you'll do integrated testing, where you tests how well your classes work together.
Of course, unit tests and integrated tests only catch around ~25% and ~45% of errors respectively. The best way to catch errors is to do code reviews where you sit down with someone and explain how and why your code works, which catches ~55% of errors. Hope you receive a satisfying result on your midterm!