Once I got tired of fixing bugs takes me more time than developing an application, and in search of solutions I came to TDD - Test-driven development (
Development through testing ).
This article describes how to take the first steps in XCode 4.5 using unit tests, when developing applications for IOS.
The article is intended for beginners, it does not contain information for bison development.
')
Introduction
Let's start from the beginning, create a new project, tick the “Include Unit Tests” box and name it Ocu:
file -> new -> project -> single view application

I prefer not to use ARC, as I adhere to the rule that new features should lie down until they are brought to mind, and it appeared only a year ago. Yes, and manual memory management is not such a complicated thing, and a little practice will be useful in this.
We have created a project in which the SenTestingKit framework is included, this framework will help us.
In the project we see 2 groups - Ocu and OcuTests, the first one contains the code of our application, the second unit tests.

Open the file “OcuTests.m” and find the method “testExample”.
OcuTests.m
#import "OcuTests.h" @implementation OcuTests - (void)setUp { [super setUp]; // Set-up code here. } - (void)tearDown { // Tear-down code here. [super tearDown]; } - (void)testExample { STFail(@"Unit tests are not implemented yet in OcuTests"); } @end
setUp is a method that will be executed before testing, here you can initialize all the objects that we need for the test.
tearDown is a method that will run after testing is completed, here you can destroy all the objects we use.
testExample is the very first test that we will perform; it contains a simple macro that will cause an error message and write that we have not yet created a single unit of test.
In general, if the method name starts with test and it returns void, then SenTestingKit automatically recognizes it as a test and executes it.
Run the test (by pressing “cmd + U” or by pressing the “Run” button and holding it, we select “Test” from the drop-down list) and see what the debugger writes to us.
Test Suite 'OcuTests' started at 2012-11-22 22:27:01 +0000. Test Case '-[OcuTests testExample]' started. /Ocu/OcuTests/OcuTests.m:29: error: -[OcuTests testExample] : Unit tests are not implemented yet in OcuTests Test Case '-[OcuTests testExample]' failed (0.000 seconds). Test Suite 'OcuTests' finished at 2012-11-22 22:27:01 +0000. Executed 1 test, with 1 failure (0 unexpected) in 0.000 (0.000) seconds
Debagger wrote that 1 test was performed, and the test was not passed, and gave us a description that we put in the macro.
Now we know where to write the test, how to run it and where to see the result of the execution. Let's start writing the easiest test.
Example 1 - addition check
In this example, we simply create 2 variables and check the result of their addition.
We define 2 variables that we will add, and the method that will contain our test.
OcuTests.h
#import <SenTestingKit/SenTestingKit.h> @interface OcuTests : SenTestCase { float foo; float bar; } - (void) testMathAdd; @end
In the setUp method, we initialize the variables and assign a value to them, and in the testMathAdd method we check the result of the addition.
OcuTests.m
- (void)setUp { [super setUp]; foo = 2.0; bar = 5.0; } - (void)tearDown {
Run the tests "cmd + U" and see an error message with a comment.
Test Suite 'OcuTests' started at 2012-11-23 12:26:14 +0000 Test Case '-[OcuTests testMathAdd]' started. /Ocu/OcuTests/OcuTests.m:30: error: -[OcuTests testMathAdd] : "foo + bar == 6.0" should be true. 2.000000 + 5.000000 should be 7.0 Test Case '-[OcuTests testMathAdd]' failed (0.000 seconds). Test Suite 'OcuTests' finished at 2012-11-23 12:20:42 +0000. Executed 1 test, with 1 failure (0 unexpected) in 0.000 (0.000) seconds
The comment indicates that the result of addition is different from the expected, correct the error.
OcuTests.m
- (void) testMathAdd { STAssertTrue (foo + bar == 7.0, @"%f + %f should be 7.0", foo, bar); }
Run the test and see the message that the test is passed.
Test Suite 'OcuTests' started at 2012-11-23 12:26:14 +0000 Test Case '-[OcuTests testMathAdd]' started. Test Case '-[OcuTests testMathAdd]' passed (0.000 seconds). Test Suite 'OcuTests' finished at 2012-11-23 12:26:14 +0000. Executed 1 test, with 0 failures (0 unexpected) in 0.000 (0.000) seconds
Example 2 - calculating the value of an object property
In this example, we will create an object, assign values ​​to its properties, and check if creation is another property correctly.
Add a new class to the project - select the “Objective-C class” template and select “Subclass of NSObject”, enter the name “Triangle”, and save it with a checkmark “Targets OcuTests”. As a result, in the OcuTests group we get 2 new files “Triangle.h” and “Triangle.m”.
In them we create the necessary methods.
Triangle.h
#import <Foundation/Foundation.h> @interface Triangle : NSObject { float cathetus1; float cathetus2; } - (id)initWithCathetus1:(float)cat1 andCathetus2:(float)cat2; - (float)getHypotenuse; @end
Triangle.m
#import "Triangle.h" @implementation Triangle - (id)initWithCathetus1:(float)cat1 andCathetus2:(float)cat2 { cathetus1 = cat1; cathetus2 = cat2; return self; } - (float)getHypotenuse { float hypotenuse = hypotf(cathetus1, cathetus2); return hypotenuse; } @end
And accordingly we make changes in the tests.
OcuTests.h
#import <SenTestingKit/SenTestingKit.h> @interface OcuTests : SenTestCase - (void)testTriangleHypotenuse; @end
OcuTests.m
#import "OcuTests.h" #import "Triangle.h" // , @implementation OcuTests - (void)setUp { [super setUp]; } - (void)tearDown { [super tearDown]; } - (void)testTriangleHypotenuse { float cat1 = 3.0; float cat2 = 4.0; Triangle *tri = [[Triangle alloc] initWithCathetus1:cat1 andCathetus2:cat2]; STAssertTrue([tri getHypotenuse]==5.0, @"Hypotenuse should be 5.0 with catheti: %f, %f", cat1, cat2); [tri release]; }
We carry out the test and get a message about the passed test.
Test Suite 'OcuTests' started at 2012-11-23 14:25:39 +0000 Test Case '-[OcuTests testTriangleHypotenuse]' started. Test Case '-[OcuTests testTriangleHypotenuse]' passed (0.000 seconds). Test Suite 'OcuTests' finished at 2012-11-23 14:25:39 +0000. Executed 1 test, with 0 failures (0 unexpected) in 0.000 (0.000) seconds
PS:
If you would be interested to read the following article, please write what examples for testing you would like to see.