Part 5 - Testing
Iteration (WHILE, DO-WHILE, FOR) Statements
When testing a loop, provide test data that will execute a
varying number of iterations, including (if possible) zero
iterations, one iteration only, and multiple iterations.
- This WHILE loop computes the greatest common divisor of a
and b.
Sample test cases for zero, one, and many iterations,
respectively, are: (a = 3, b = 3), (a = 2, b = 1), and (a =
81, b = 54).
while (a != b) {
if (a > b) {
a = a - b;
}
else {
b = b - a;
}
}
- Examine the code in Lab09B.loopMethod(), then
write test cases in Lab09B_Test.testLoopMethod()
that will execute the loop zero, one, and multiple times.
Also make sure that your test cases execute each branch of
the if statement included in the loop.
- After you've finished this step, zip Lab09B.java and
Lab09B_Test.java, submit the zip file to WebCAT, and then
examine the code for Lab09B.loopMethod() on WebCAT to ensure
that it was fully covered. Any red lines indicate a lack of
coverage. Revise your tests and resubmit if necessary.
Part 6 - Black Box Testing
It's also necessary to test code according to its
specifications (i.e. what it's supposed to do) rather than how
it works inside. Specification-based testing is also called
black-box testing, since testing is based on documentation
only and the inside of the code either can't be seen or is
ignored. This type of testing requires that every possible
scenario described in the documentation for the code be
examined with one or more test cases. Black-box testing also
entails trying to "break" the code by creating test cases that
the programmers might not have foreseen.
- The documentation for the method squareRoot() in
Lab09B.java explains its purpose. Create a variety of test
cases for this method in Lab09B_Test.testSquareRoot(),
ensuring that you cover every possibility. Also try to
break the method with weird test cases.
Remember that this method promises only limited precision.
So, for example, the square root of 4.0 won't necessarily
be 2.0, but the absolute value of the difference between
the two should be within the stated precision. Absolute
value can be computed with the function Math.abs(). You
should use the TestCase method assertTrue() rather than
assertEquals() to test that the absolute value is less
than or equal to the stated precision:
assertTrue("...error
message... ", Math.abs(expected - actual) <=
PRECISION);
- After you've finished this step, zip Lab09B.java and
Lab09B_Test.java, submit the zip file to WebCAT, and then
examine the code for Lab09B.squareRoot() on WebCAT to
ensure that it was fully covered. Any red lines indicate a
lack of coverage. Revise your tests and resubmit if
necessary.