Information Technology Reference
In-Depth Information
An error string, which optionally can contain standard printf/NSLog parameter placeholders with
formatting codes
The parameters to be logged, if there are any
In the example code, the elements look like this:
STAssertTrue . Test if the following conditional evaluates to true. Write an error to the build log if it
doesn't.
testMathMachine.sumAB == kExpectedSum . This is the conditional that implements the test. It
checks whether the expected sum constant matches the sum returned by the initWithSum: method in
the previous line.
@”Sum incorrect. Expected %i, got %i”. This is the text of the message that appears in the
build log when the test fails. It takes two integer parameters.
kExpectedSum, testMathMachine.sumAB . These two parameters are reported in the message.
If the test fails, the current value of the parameters is logged as part of the message.
When you create test code, you must select a macro from the table, add a suitable expression, and fill in the
message and parameter details as needed. Without a test macro, your test code does nothing.
You can test for multiple error conditions by including multiple macros in a single test method. You can and
should include multiple independent test methods. In a real example, each test case method should test a spe-
cific feature in your code. When you add a new feature or correct a bug, add a corresponding test case. You can
also use these macros to generate messages that confirm when tests have been passed.
TIP
The assert macro syntax is used throughout development in many environments. If this is your first encounter
with it, it can seem unintuitive because assert assumes negative logic and does the opposite of what you'd ex-
pect it to in English. It checks a condition and logs an error if the condition isn't true. When you use these macros,
it can be helpful to think of “assert” as equivalent to “test if.”
Running tests
When you save the code, the tests are almost ready to run. But Xcode requires one final step before it can run a
test build successfully.
By default, classes that are being tested aren't included in the test bundle. If you try to run a test build, Xcode
can't find the headers or the code for them.
To fix this, select the UnitTestTests target, as shown in Figure 17.8. Select the Build Phases tab, and open the
Compile Sources pane. Click the + triangle to add a new source, and select the MathMachine.m file, as shown
in the figure. This adds the source file to the build and ensures that the test framework can see the class you're
trying to test.
When you are testing multiple classes, you must add them all. Add UI management classes such as view con-
trollers only if they're referenced in the classes that are being tested.
Search WWH ::




Custom Search