|
home—lects—hws
D2L—breeze (snow day)
By reflex, remember to check unit tests that include:
kneejerk value to test | example for |
---|---|
n/a | |
a string of length 0, viz. |
|
n/a | |
a string of length 1 | |
a negative number | n/a |
a fractional number | n/a |
both | n/a |
For boolean functions, an input that will return | n/a |
For boolean functions, an input that will return | n/a |
a “typical” input. |
In addition, you'll test cases pertinent to the code:
A function dealing with names may want to check hyphenated-names,
reasonably long names, short names (one letter).
A function dealing with different tiers of prices (less than $25, less than $100, and above)
should include cusps ($24.99, 24.995 (if possible), $25.00, $25.01, and so-on).
For
blend( "abcdef", "wxyz" ) // expect "abcyz" blend( "abcdef", "xyz" ) // expect "abcz" blend( "abcde", "wxyz" ) // expect "abyz" blend( "abcde", "xyz" ) // expect "abz" |
Here is the full file, and the result of running it (show-source).
Put your trivial test-cases first. (That is, the ones involving 0, the empty string, etc.) — if one of them fails/crashes, they're the simplest to hand-trace and figure out what's happening.
Suppose, in writing blend, all our tests return 0 (?!).
It's easier to trace through arithmetic of
The one exception is if you're also using your tests for documentation. Having one typical-use case (or two or three) is extremely effective at helping readers understand what your function does. (In full frameworks, your documentation and test systems should share info, but in quick-and-dirty settings, then your first couple of test cases might double as example-documentation.
We've only mentioned unit tests, and that's all we'll focus on for our code. However, there are many more approaches to testing, which all help reduce the bugs in your code (and the amount of time you spend banging your head on the wall), I recommend ITEC 335, Software Testing.
1 A type-safe language should catch this sort of error; if using a non-type-safe language, or one with liberal auto-type-coercion, then Turing help you. …You might add macros or pre-processing to your source code, to catch type errors in this case. For our class, using php (with dangerously liberal auto-type-coercion), we'll ignore this, since it's not in the scope of a web-programming class. This requires extra vigilance on you as a programmer, to be sure you're providing the right types. ↩
home—lects—hws
D2L—breeze (snow day)
©2015, Ian Barland, Radford University Last modified 2016.Jan.19 (Tue) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |