|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 4
Even though I've given you the code first, in general it's a good idea to take a stab at writing some tests before you begin actually programming. While it is difficult to generate a thorough and comprehensive test suite at this point, there are several good reasons for taking a first stab at it:
After you've written some code, it's easy to return to the test suite and add additional tests. In addition to testing any new requirements you've discovered, these tests should test all paths through the unit under consideration (keeping in mind the discussion of black-box versus white-box testing two months ago).
Now, let's look at the test code:
<?xml version="1.0" encoding="US-ASCII"?>
<!DOCTYPE testsuite SYSTEM "test.dtd">
<testsuite name="Scanner Test">
<preamble>
<set id="scanner"><constructor class="Scanner"/></set>
</preamble>
<test name="Test 1">
<preamble>
<set id="vector"><constructor class="java.util.Vector"/></set>
</preamble>
<action>
<set id="return">
<method id="scanner" name="scan">
<get id="vector"/>
<primitive>
/**
* The _Role_ interface.
*
* A role organizes a set of permits into a package.
*
* @author Todd Sundsted
* @version 0.1
*
*/
</primitive>
<primitive>0</primitive>
<primitive>-1</primitive>
</method>
</set>
</action>
<result>
<equal>
<get id="return"/>
<primitive>136</primitive>
</equal>
<equal>
<method id="vector" name="size"/>
<primitive>2</primitive>
</equal>
<equal>
<method id="vector" name="elementAt"><primitive>0</primitive></method>
<primitive>@author Todd Sundsted</primitive>
</equal>
<equal>
<method id="vector" name="elementAt"><primitive>1</primitive></method>
<primitive>@version 0.1</primitive>
</equal>
</result>
</test>
</testsuite>
The tests above are written in XML. I'll explain the syntax later; let's first concentrate on understanding what it is we're testing.
Tests are grouped into units called test suites. Each test has three sections. The action section contains the action or behavior being tested. It typically contains either a constructor (if the construction of the unit is what is being tested) or a method. The result section contains the conditions that determine whether or not the unit passed the test. The preamble provides a place in which to set up the objects used in the test.
The suite of tests above is by no means complete. A complete test suite can include dozens or even hundreds of individual tests. But our sample tests tests do demonstrate how to use the testing framework and how to construct tests.
If you look closely at the code in the Java class above, you'll notice a mistake. Let's see what happens when we run the test suite.
Test Suite: Scanner Test Test: Test 1 ...passed ...passed ...failed ...failed
The output indicates which test suite ran and which test identified a defect. Defects are identified by comparing the generated
output with the expected output as described in the <result> section of each test (more on which below). The failed test indicates which input caused the unit to fail. In our case, problems
arose with the last two conditions of the first test.