Information Technology Reference
In-Depth Information
In software engineering a contract is an explicitly defined interface between two elements. The two elements are
designed to exchange information in a certain way, and the contract defines the details of the ex-
change—specifically the data format, timing, and sequence of the exchange.
Unit testing is designed to help with some of these problems but isn't a solution for all of them. Philosophically,
unit testing is closer to method and function testing than sequence testing. To use unit testing successfully, code
should have clearly defined interfaces and predictable outputs.
A key benefit of unit testing is that if a bug is easy to reproduce, it's easy to test for it. Creating a test guarantees
that future bug fixes don't reintroduce problems. Unit testing can help make these regression errors less likely.
Another benefit is that with minor modifications, test code can be used as example code in documentation, to
illustrate how features are designed to work.
You also can use unit testing as a design aid. If you test as you go, you can catch logical inconsistencies and
overly complex contracts before you implement them. Potentially, you can use test code to sketch how a feature
should work before you implement the code for the feature.
In spite of the advantages, unit testing remains controversial. There's an approximate consensus that unit testing
is most effective in collaborative projects with a well-defined API. Solo programmers are more likely to have an
overview of their project than group developers, so objective testing of elements can sometimes be more of a
distraction than a benefit. As interaction becomes more GUI-driven and open-ended, unit testing code can be-
come so large that it rivals the size of the main application, and the return on the time and effort invested be-
comes smaller.
For all applications, unit testing is most effective when combined with intensive beta testing and formal bug
tracking. The most robust production regime combines beta testing, unit testing, and aggressive defensive cod-
ing that anticipates and codes around likely input errors.
Understanding Unit Testing in Xcode
In outline, you can do the following with unit testing:
Compare returned values from a method or function with expected values
Check that objects are created and initialized correctly
Confirm that error conditions throw an exception
Repeat tests any number of times
Create composite test sequences that run various tests in order
Select tests depending on the results of previous tests
When you create a new iOS or OS X project, you can choose to add unit testing features. Xcode automatically
adds the features—in fact, it adds a separate target that implements unit testing—and initializes them. However,
the initialization isn't comprehensive, and extra work is required before you can begin adding test code. The
work isn't described in the official documentation, but it is listed below.
In detail, the unit testing package includes the following:
Search WWH ::




Custom Search