I have been delaying in learning Unit Testing ever since the released of XCode 5 on September 2013. It is partly due to my busy schedule in rushing 2 complex projects back to back. Now the Second project – Watch Over Me 3.0 is under the beta testing stage, so, I have some time to pick up some skills that I have been wanting to learn.
What is Unit Testing?
Unit Testing is part of Agile Software Development Methodology to make the entire software development process more efficient, the development project to be easier to maintain, less crashes on deployed software and higher quality of code.
Automatic Unit testing, on the other hand, is a proactive way to test a small unit of codes (functions, classes) from time to time to make sure that they are always ready for production. You can host a test server (eg: OS X Server) together with Git repository server. On the test server, some bots will test the latest commits of the codes frequently.
Unit Testing in XCode 5
Apple has done a great job by providing us (The iOS developer) with good framework that can help us to implement unit testing easier on XCode 5. I have learned this unit testing through WWDC 2013 video – Testing in Xcode 5 by Mike Swingler.
From what I learned from the video, we all should start the Unit Testing from Model and Controller. XCode 5 has been designed to make the Testing between the Model and Controller really easy. The new framework for the testing is XCTest.framework. This framework is derived from OCUnit. The iOS developers have been using OCUnit framework prior to XCode5. So, the previous testing codes are compatible with XCTest framework. If you were using OCUnit, you only have to refactor your old code to make it running well with the new framework.
While I was trying to implement the Unit Testing on my old personal Calculator XCode project, I found this simple tutorial (CS305 Software Engineering – Unit Testing in iOS). It shows me some steps and codes using a different calculator project. I learned something new from it.
Be warned, this project is old and it is definitely not optimized for iOS 7.
Example of Unit Testing in Calculator App
I just implemented what I have learned from the video on an old Calculator app project that I have developed for fun some time ago. This is not a usual calculator that most people are familiar. This is a RPN (Reverse Polish notation) calculator.
For example, if you would like to calculate an operation such as 21 + 63, the steps that you have to follow are:-
1. Press 2 and 1
2. Press Enter
3. Press 6 and 3
4. Press Enter
5. Press +
6. Press Enter
You will then get the result showed like the screen below:-
Unit Testing can be further divided into 2 parts:
1. Logic Unit Testing
– Test only the logic with fixed values and without human interaction
2. Application Unit Testing
– Simulate the user inputs by using user interface such as button and make sure that the app behaves the way it should.
To keep make thing simple, I will only implement Logic Unit Testing for now. I will add more examples when I learn deeper into this Unit Testing.
The Model of my calculator project is CalculatorBrain. It has 3 functions:-
1. pushOperand
2. performOperation
3. popOperand
So, I will perform the unit testing on these functions.
There are only 4 major operations:-
1. Addition (+)
2. Subtraction (-)
3. Production (*)
4. Division (/)
I will implement Logic Unit Testing for all these 4 operations but I will only show an example in this article.
When a user is trying to add 21 with 63, he/she should get the result as 84. Or else, this operation will fail. Here are the steps:-
1. I import CalculatorBrain.h into CalculatorTests.m.
2. I create an instance of CalculatorBrain and instantiate it in setUp method.
3. I create a method calls testAddition.
4. I push 2 numbers (21, 63) using pushOperand.
5. Then, I use performOperation to calculate the result.
6. Lastly, I compare the result with 84 using XCTAssertTrue.
– (void)testAddition
{
[self.brain pushOperand:[@”21″ doubleValue]];
[self.brain pushOperand:[@”63″ doubleValue]];
double result = [self.brain performOperation:@”+”];
XCTAssertTrue(result==84, @”Addition Fail”);
}
– (void)testAddition { [self.brain pushOperand:[@”21″ doubleValue]]; [self.brain pushOperand:[@”63″ doubleValue]]; double result = [self.brain performOperation:@”+”]; XCTAssertTrue(result==84, @”Addition Fail”); }
If the logic testing is good, it will show you the following result on the command line:-
Test Case ‘-[CalculatorTests testAddition]’ started.
Test Case ‘-[CalculatorTests testAddition]’ passed (0.000 seconds)
If the logic testing is wrong, it will show you something like:
Test Case ‘-[CalculatorTests testAddition]’ started.
/Developer/ Calculator/ CalculatorTests/ CalculatorTests.m:38: error: -[CalculatorTests testAddition] : ((result==82) is true) failed – Addition Fail
Test Case ‘-[CalculatorTests testAddition]’ failed (0.000 seconds).
You then will know that you have made a mistake somewhere in the code, you will have to find a way to fix it.
The screen below shows you the complete unit testing for all the 4 operations.
Final Thought
Unit Testing is really an interesting way that helps us to be more proactive in programming. This is especially true when we are able to implement the Bots that help us in testing the project automatically.
You can find the full source code for this simple Unit Testing that I uploaded to Github here: iOS Unit Testing By Using Calculator Project on XCode 5.
Enjoy!
Leave a Reply