|
Review: show the result of requesting hello-world_php.txt (result: hello-world.php).
The server, seeing the suffix “.php”, is configured (presumably) to
run that file as a php program, and whatever that program prints is what the server
sends back to the client.
SO: when our browser visits on hello-world.php, and we do a show-source, do we see the php program? NO! (Why not? Let's reread the above paragraph!)
Wait: So how am I displaying the source, if the server doesn't send you the program? For this one example, I happened to make a copy of the php file, and re-named it ending in “txt”. When requesting that file, the server did not consider it a php program and didn't run it; instead it just returned that filed. It looks funky; do a show-source and then tell me why the client is displaying the source-code in a very odd way.
Task:
the function
For any programming task (from 120 on up, including web programming), we start with test cases, which are runnable code.
echo "actual: ", blend("motor", "hotel"), "\n"; echo "expect: ", "motel", "\n"; echo "actual: ", blend("smoke", "fog"), "\n"; echo "expect: ", "smog", "\n"; |
Here is the completed php file, and the result of running it (show-source).
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. |
Note: Here's something beyond what we'll consider in this course, but in general it's worth realizing that one can compile a “big list of naughty strings”, for use in automated or manual testing.
Finally:
In addition to these knee-jerk test cases,
you'll of course test inputs pertinent to the particular funciton your write:
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" |
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.
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |