Wednesday, April 16, 2014

Unit testing iOS code - SenTesting framework

Apple gives definition of unit testing as a way to ensure that the code you write adhere to the design specifications and it keep adhering to the specifications as you modify it. To me, it is almost the same and also making sure the code is without lot of bugs especially around the area those are rarely tested or executed in real world.

Xcode gives a way to include Unit test bundles. These bundles contain methods that exercise the code in a library or app to perform unit testing.

There are main three portions to it.

1. Setting up unit testing in a project
2. Writing test case methods
3. Running unit tests

Setting up the unit testing is pretty straight forward. on the project window add a new target and select the type as Other -> Cocoa Touch Unit Testing Bundle. Give the target name. In the new versions of XCode, am using the XCode 5.0 it asks for the Type of the Unit testing. The drop down has two options 1, OCUnit (Xcode 4 compatible) 2, XCTest

Selecting the OCUnit makes the SenTestingKit framework a required library to be added. This time selected the OCUnit as the XCTests is comparatively newer approach that also integrates with the Continuous Integration feature available in XCode 5.0.

Adding a new Unit Testing Target includes a set of source files which is containing one function testExample and other two life cycle functions setUp, tearDown.

Below are the instructions before running the tests which i had to do to get this working.

- In the build settings pane, search for bundle loader and test host and test host. If these are highlighted, which means these are having some values, then delete them so that they become unhighlighted. from Products -> Run, run the tests.  This was going through the setUp , testExample, tearDown methods in order and since the testExample had statement STFail, the test was declared to be failure.

Next review of this framework, i think mainly the below will be interesting

- How to write more test methods
- how to do testing that involves server responses
- How to do testing of screen flow
- How to run some test at some point of execution in the app midway.

This below article gives Good explanation of the entire setup
https://developer.apple.com/legacy/library/documentation/DeveloperTools/Conceptual/UnitTesting/02-Setting_Up_Unit_Tests_in_a_Project/setting_up.html#//apple_ref/doc/uid/TP40002143-CH3-SW1



No comments:

Post a Comment