Test-driven development is part of agile development. It generally involves the steps of "red-green-refactor ".
IOS testing-driven development tools
I. ocunit is a test tool provided by xcode.
The procedure is as follows:
1. Create a test target.
The test targets can be divided into logic test and application test, where the logic test can be executed independently and the application test needs to start the app.
For application testing, builder setting needs to configure the value of bundle loader and test host so that it can be used to change the target.
Set
The value of the bundle loader build setting:
IOS: $ (built_products_dir)/<app_name>. APP/<app_name>
Set the value of the test host build setting:
$ (Bundle_loader)
For details, refer to unit testing in IOS
2. compile unit test case
The test cases of ocunit are inherited from the abstract class sentestcase. Therefore, when writing your own test classes,
1 inherited from sentestcase
2. complete the establishment of the test environment in the setup method. Generally, the instance of the test class is initialized (the execution cost of each test function will be called)
To set up different environments for different test functions in the same test class, consider using setuptestwithselector :( SEL) testmethod;
3. Write your own test functions, such as testmyexample (prefix with test is required), and use assertions to declare the expected test results.
Common assertions include:
Stassertnil asserted object is null
The stassertnotnil asserted object is null.
Stasserttrue asserted expression true
Stassertfalse the asserted expression is false.
Stassertequals asserted that the values of the two variables are equal.
Stassertequalobjects asserted that two objects are equal
Stassertthrows assertions throw an exception
Stassertnothrow exception
For more assertions, see sentestingkit/sentestingutilities. h.
4. Restore the test environment in teardown (each test function is called after execution)
3. Run the unit test
Command + u
For details, refer to the official documentation.
Ii. ghunit
Test Framework, Visual Test Result Display
Ghunit
Ghunit configuration address
3. Test-driven development the most important thing is to write high-quality unit test cases. The code written according to the MVC design pattern is structured. This can also be applied to write unit test, for the test case of the model layer, the general structure is as follows:
@class Topic;@interface TopicTests : SenTestCase { Topic *topic;}@end
In the topictests class, create and initialize a topic, and test the topic class behavior through the happy result and boundary value.
The Controller layer is generally more complex than the model layer and involves multiple objects. The first step is to isolate coupling,
For Asynchronous Network tests, mock objects that do not send network requests must be used,
For the view layer, unit test covers less,
Iv. Test Coverage Rate
For students above xcode4.0, the output coverage result is set as follows:
Under the target, go to the build setting interface.
Set Generate Test Coverage files: Yes
Instrument Program Flow: Yes
This is/users/<username>/library/developer/xcode/deriveddata/<app_name-<serial>/build/intermediates/<app_name>. build/debug-iphonesimulator/<target_name>. build/objects-normal/i386
The. gcda and. gcno files in this folder will be sent. At this time, xcode llvm will output the test results, download a coverstory, open the corresponding file, and you will see