Information Technology Reference
In-Depth Information
Each method is called a test case . As explained earlier, it's up to you to create test code that exercises the fea-
tures of your target class in the most logical and comprehensive way. In practice, this requires three steps:
1. Define the inputs to the test and the results they should generate.
2. Add a test case method to the Tests class header.
Depending on your test code, you may need to define the inputs and expected results as constants.
3. Implement the test method in the Tests implementation file.
Use assert macros, described below, to implement the tests and report errors.
4. Optionally, add set-up and tear-down code around the test.
The Tests implementation file includes predefined setUp and tearDown methods to hold this optional
code. You can create and release test objects in these methods, or you can build alloc and release calls
directly into each test case. The best solution depends on the test requirements.
Defining test inputs and results
Figure 17.6 shows the header of the test class. You can see two predefined inputs, kA and kB, and a pre-
defined expected sum, named kExpectedSum . The testMathMachineSum method runs the test that
compares them. Here's the code:
#import <SenTestingKit/SenTestingKit.h>
#define kA 1
#define kB 1
#define kExpectedSum 2
@interface UnitTestTests : SenTestCase {
@private
}
-(void) testMathMachineSum;
@end
The #define statements and the testMathMachineSum method have been added. The other parts of the
file are created with the project. Note that the method doesn't take parameters, and there are no semicolons
after the #define directives.
This example is trivial. In a more realistic test case, the relationship between the expected result and the inputs
would be less obvious. It might rely on a series of object allocations and other complex operations. Potentially,
you could predefine an array holding a sequence of input events in the setUp method and sequence through
the array in the test code implementation. Inputs and expected results might be downloaded as a file from a re-
mote server and created by other members of a development team.
FIGURE 17.6
Defining test inputs, an expected value, and a test method
Search WWH ::




Custom Search