Introduction to Testing on iOS



For delivering a bug free app , you must need the app to be pass by several type of testings.

There are many tools available that can be used to automatically test the application we have write, but some of the tools are an open source model, but in case of Xcode, apple provide its own SDK for testing which improve with Xcode versions and help developers to test the code they write.

In this tutorial we are creating a demo app that uses apple provided tool for automated testing,XCTest. XCTest is apple’s unit testing framework, for those who are not familiar with unit testing, it is the type of automated testing the verifies code at the lowest level.

Here we write Objective-C code for checking an input string is palindrome or not.

At test class it verify the variables set correctly and the return value is correct or not!

Tests we write in XCTest framework executed repeatedly in application code which helps a developer to increase his / her confidence to write a bug free code and verify its not breaking and to measure its time of execution.

At apple XCTest it provides three things

1) A separate target for tests

2) A group of test classes

3) An example test

NOTE: The method name always begins with test.

for example : -(void) testDosomething { }

Creating First iOS Unit Test

  1. Create  A demo App
  2. Open Xcode
  3. Create new project (File -> New ->Project -> Select IOS -> Application ->Single view Application)
  4. I have name this project  as UnitTestExample
  5. Below  Language  there are three options

         i ) Use Core data

         ii ) Include Unit Tests  (Check box it )

         iii ) Include UI Tests   

  



  1. 6) It creates TestCase_9to5iOSTests group and  TestCase_9to5iOSTests.m classes in the project

Or if you want to add test class manually in your existing project then follow

1) File->New->Target->Scroll Down Under Test Select iOS Unit Testing Bundle and name it TestCase_9to5iOSTests



 

Inside your ViewController.m, i have place reverseString method

-(NSString*)reverseString:(NSString*)testString

{

    int len =(int) [testString length];

    NSMutableString *result = [[NSMutableString alloc] initWithCapacity:len];

    for (int i = len – 1; i >= 0; i–) {

        [result appendFormat:@”%c”, [testString characterAtIndex:i]];

    }

    return result;

}

Import the header file of ViewController class and add a property of type ViewController to the TestCase_9to5iOSTests.m

@property (nonatomic) ViewController *vcToTest;

After it now In the

– (void)setUp method , we have to initialise the property as shown below

– (void)setUp {

    [super setUp];

        self.vcToTest = [[ViewController alloc] init];

    // Put setup code here. This method is called before the invocation of each test method in the class.

}

Inside TestCase_9to5iOSTests.m

we over-ride  function testExample and testPerformanceExample

Also we can create our own method inside it starting with test word for example : -(void)textMyCase

– (void)testExample

{

    NSString *testString = @”9to5iOS”;

    NSString *ResultReverseString = [self.vcToTest reverseString:testString];

    

    NSString *expectedReverse = @”SOi5ot9″;

    XCTAssertEqualObjects(expectedReverse,ResultReverseString, @”Expected Reverse String Match with result reverse String”);

                          

                          

    // This is an example of a functional test case.

    // Use XCTAssert and related functions to verify your tests produce the correct results.

}

– (void)testPerformanceExample {

    NSString *testString = @”9to5iOS”;

    // This is an example of a performance test case.

    [self measureBlock:^{

        [self.vcToTest reverseString:testString];

        // Put the code you want to measure the time of here.

    }];

}

To Test press Command + U as shown

 

And here is your output with green check box and measure performance block

 







 

Download source code here 



Add a Comment

Your email address will not be published. Required fields are marked *