Still doing manual unit testing? – XCTest

These days more and more companies prone to Test driven development(TDD) as I were asked by 3 companies if I have experience in this. I said no as I had been using manual testing for my development. Well, I heard of it(in my uni) but I had never implied as my previous startup company just let me code, test and shout at me when something gone wrong which I had no idea.

Here comes various testings which I’m glad I am come to know of them:

 

1. XCTest

Before XCtest, there were OCunit and GHunit testing which dated back 2010 in Xcode 4. Looking at the APIs provided for GHunit, it looks similar as the XCtest APIs except now XCtest now is built-in the Xcode 6.

I need to test for my static library project but for iOS, logic testing cannot be displayed on real device(only in simulators) which was a real disaster for me. In my static library, it embedded a customised framework which MUST build in a real device. Nearly retorted to use GHunit as it can be used on real devices despite it is severely outdated as I am the junior iOS developer and ONLY iOS developer (So I have no one to turn to except keep searching stakeoverflow and forums).

After a lot of time searching around and failed configurations (a lot of linker errors) , here are the steps I get my static project to work:

XCtest for static library

Step1: Add a new target

Step2: Create a single view application as static library should be wrapped in an application to testing. Don’t worry, you can still navigate into your static library method when you want to trace the flow with breakpoints.

Step3: Add all the related libraries that needed to be used in the new target project and project test at link binary with libraries.

XCTest2

Step4: Ensure target dependencies and the compile sources linked correctly.

XCTest3

Step5: Remember to set active scheme to your test project

*Note: If you still encounter linker error when you may need to link the libraries(Step3) in the testprojectTests.

 

Code:

Pre-run test method

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
    DeviceClassObject *deviceMgr = [DeviceClassObject alloc]init];
}

Post-run test method

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

 

Inside test method

#pragma mark - GET DEVICE LIST FROM SERVER
//Test return response between 300 - 306
- (void)testDeviceCheck{
    DeviceClassObject *deviceMgr = [DeviceClassObject alloc]init]; //You can put this in the setUp method if you wish to initiate before each test method
    int deviceResponseInt = [deviceMgr getReturnIntResponse]; //Run test on the method you want to test
    NSLog(@"deviceResponseInt : %i", deviceResponseInt);
    
    //1. test for greater than or equal
    XCTAssertGreaterThanOrEqual(deviceResponseInt, CHECK_MAX_SUCCESS, @"Should not less than %i", deviceResponseInt);
    //2. test for less than or equal
    XCTAssertLessThanOrEqual(deviceResponseInt, CHECK_MIN_SUCCESS, @"Should not more than %i", deviceResponseInt);

    //3. test for not equal
    XCTAssertNotEqualObjects(deviceResponseInt, 0, @"Should not return 0"); //deviceResponseInt should not equal to 0

    //4. test for equal
    XCTAssertEqualObjects(deviceResponseInt, CHECK_MAX_SUCCESS, @"Device should be in list"); //CHECK_MAX_SUCCESS should equal to deviceResponseInt

}

There are much more tests can be conducted such as performance test (will be updated in the future)

 

FAQ:

testing

1. Should I change my codes for test driven development?

Yes, you should. You should change your code for the long term.

2. How should I name my test method?

Every test case should started with the word “test”, etc: “testDeviceCheckInListPass”, “testDeviceCheckInListFail” .

Refer:

XCTest tutorial – codeTutsPlus

Difference of OCUnit vs GHunit vs XCtest

Testing in Xcode 6 – WWDC

Testing in Xcode 5 – WWDC

XCTest documentation – Apple

 

2. UI test – UIAutomated testing (To be continued)

 

 

 

One thought on “Still doing manual unit testing? – XCTest

Leave a comment