Unit Testing in 4 iPhone, Xcode 4 Unit Testing in Quick Start Guide iPhone



Unit Testing in 4 iPhone, Xcode 4 Unit Testing in Quick Start Guide I Phone

Unit Testing is too much required because it is very helpful for our quality. Easier so that we can deliver high quality of code and also easier to done edits without any tension of breaking anything.

But what is not easy here, that is those who didn't started so we are providing here for those who are the new for this. This tutorial is all about for those.

Xcode project set up for unit testing
Xcode project set up for unit testing


Here we shall cover up how we can set up the Xcode to use following three different -2 frameworks of unit testing:

  • OCUnit : This is unit testing framework created in Xcode
  • GHUnit : This is the third party frameworks with some other cool features.
  • OCMock: This is used to write the mock object to add tricky testing scenarios

Lets Starts with OCUnit:
Build straights into Xcode is used to create unit testing frameworks is called OCUnit, So we will try it first. In Xcode, Visit on following location → File \ New \ New Project, Now just select the iOS \ Application \ View base Applications and then click on Next. Now provide the name of project as TestProject, Also need to confirm the check the option of Include Unit Test, Please see the below screenshot -:

 Xcode Project Unit Testing
Xcode Project Unit Testing


Now click on Next and Select a required folder on specific place then Click Create.
You can see at created project, you shall see that Xcode has been created two targets for us: Sample Project (The app target) and Sample Project Tests(the unit test target)Unit test target is dependent on app target. This way when we run the test the apps target will automatically built up.

Unit Testing in 4 iPhone
Unit Testing in 4 iPhone


For example Single test class has been created by the Xcode for you, which can be fine in SampleProjectTest \ SampeProjectTest.m. You shall watch a single tests case se up in available file which will look like this :

- (void)testedExample
{
STFail(@"Unit test is not implemented yet in SampleProjectTest");
}

This basic sample test should fail on the first step of execution becaue unit tests are not created till yet.
So let us try to do this and watch if it required fails as it should. Select your iPhone simulator from the Scheme drop down, Now choose Projduct \ Test from the Xcode manu.
Apps target will be build by the Xcode, then again build the target of unit test. If both targets can be built it then runs the test cases on the simulator and highlights any failures in the Issue Navigator and in the source file itself, just like it does with build warnings and errors.
So as we can see that setting for unit testing with the help of OCUnit in Xcode is not hard and its really easy task. So it is just a thing of select the check box.

Why we prefer GHUnit instead of OCUnit:

GHUnit will allow us to execute all types of test cases, a single test or just the failed test but OCUnit will allow only execute all of them.
The category of GHUnit is the neat test runner apps, so it quickly show the high level of view passing and fail tests. While OCUnit does not have such thing.
GHUnit is like a open source project so we can edit the frameworks to make it better to meet our requirement.

Now OCMock: Mock objects allow us to tests the interaction with the outside world while keeping external dependencies as low as possible. If our code has external dependencies or responsibilities (and most do), then we shall want to use these!

So OCMock framework for OSX and iOS develop by Mulle that follow the parrerns of mock framework develop for other platform.

So we shall learn how to set up the Xcode to uses this along with the GHUnit.

GHUnit and OCMock Installation:Instruction provided here will help to create a structure of project with all such files saved in the real project directory, containing GHUnit and OCMock framework. We assume these instructions are those you create or download in the directory ~/myproj, but feel free to put them wherever we want.

After each steps in the running process, we should need to keep clean, build and execution for each targets to validates the configurations and dependencies.

Now lets Started: Creating fresh directory and the project
In the home directory just need to create sub -directory myprojTests
Open the Xcode and generate the new view base application
Just leave the “Include Unit Test” check box unchecked since you will be using the GHUnit instead of OCUnit
Now finally Save the project as myprojTests in ~/myprojtests
Confirm that project builds and executes in the simulator without any issue or error

Integrating GHUnit

Let's integrate it -:
  1. Add a GHUnit Test Target:- First of all you required to add to our project for GHUnit. From the Project Navigator View select the project file, and then click on the Add Target target test.
  2. Now Create an iOS View based application named 'MyProjTest' for the GHUnit target. Just leave the contains unit test check box unchecked.

Add GHUnitIOS Framework:
Our Next step is to adding GHUnit Framework. Make sure that selected is the latest iOS version which you are going to use.
We required to includes the full set of file into our projects.

  1. Configure GHUnit Test The Target: The frameworks of GHUnitIOS has an app delegate and embedded window, so we require to removed the ones installed by default whenever create the target of test.
  2. First, delete the all files MyProjTestsAppDelegate.h, MyProjTestsAppDelegate.m, MainWindow.xib, MyProjTestsViewController.h, MyProjTestsViewController.m, MyProjTestsViewController.xib, and main.m from MyProjTest
Next, remove the “Main nib file base name” property from MyProjTests-Info.plist.
So Now, added the files to the MyProjTests target, and make sure to select the MyProjTests target when adding the files.
Finally, You required to update the build setting for the MyProjTests target. Now Select “Other Linker Flags” under MyProjTest and added the value “-ObjC -all_load”
Congratulations – you have successfully installed GHUnit! Execute the MyProjTest target in the simulator and you shall watch the GHUnit test runner:

Installation of GHUnitIOS.framework
Installation of GHUnitIOS.framework



Lets Create Simple Test Case for iPhone:
AS we not not any test case till yet but GHUnit has been integrated previously. So now let us make a simple test case for above work.

First need to create a New Objective-Cclass Sample Test Case.m in the MyProjTest group, to make sure that it belong to the MyProjTest target, as it not to the MyProj targets.

See out test cases classes doesn't require a header file. Actually Xcode 4 doesn't allow us the option to create only a .m file, so need to remove the SampleTestCases.h file and also need to change the entire content of the SampleTestCase.m with the provided following code:

#import <GHUnitIOS/GHUnit.h>

@interface SampleLibTests : GHTestCases { }
@end

@implementation SampleLibTests

- (void)testSimplePasses {
// Another test
}

- (void)testSimpleFail {
GHAssertTrue(NO, nil);
}

@end

Now just press the Run button from the top right corner to execute the testsHere one test case should succeed and one should be fail.
Creating a new test cases in GHUnit is the straight forward, such test cases have simply a method that follow the following simple rules:

Classes inherit from the GHTestCase
Return type of method is void
Method name begin with the 'test',
and function take no argument

Obviously, to become a useful test cases, it requires to do something (unlike the 'testSimplePass' method above.)
Each test case should have a single purpose, and often after all the setup is done it only checks a single value. It can be as simple as creating an instance of the class to be tested, calling a method on that class, and checking that the value returned matches your expectations.

OCMock Addition:

Now we have the setup for GHUNit and executing, We like to submit support for OCMock. The frameworks available on the websites that does not work with iOS projects.
Let's add the OCMock →
Project Navigator in Xcode, Right click on MyProj and then choose the “Add Files to 'MyProj'”:
Choose the directory ~/myproj/MyProj, and then click on the “New Folder” button. Add a name as “Libraries” for folder and again click on the “Create” button. As soon as folder is created, Click to 'Add' to add it to the target folder or location.
Now adding the OCMock is just a three steps ahead to your project →
  1. Install the latest OCMock iOS library
  2. Extract header file from the OCMock frameworks
  3. Need to Update the build Setting for MyPRojTest

Simple Test Cases with the OCMock:

Now we need to add some test cases that use OCMock to our SampleTestCase. Update SampleTestCase.m to look like this:

#import <GHUnitIOS/GHUnit.h>
#import <OCMock/OCMock.h>

@interface SampleLibTests : GHTestCases { }
@end

@implementation SampleLibTests

- (void)testSimplePasses {
// Another test
}

- (void)testSimpleFail {
GHAssertTrue(NO, nil);
}

// simple test to ensure building, linking,
// and running test case works in the project
- (void)testOCMockPasses {
id mock = [OCMockObject mockForClass:NSString.class];
[[[mock stub] andReturn:@"mocktest"] lowercaseString];

NSString *returnValue = [mock lowercaseString];
GHAssertEqualObjects(@"mocktest", returnValue,
@"Should have returned the expected string.");
}

- (void)testOCMockFail {
id mock = [OCMockObject mockForClass:NSString.class];
[[[mock stub] andReturn:@"mocktest"] lowercaseString];

NSString *returnValue = [mock lowercaseString];
GHAssertEqualObjects(@"thisIsTheWrongValueToCheck",
returnValue, @"Should have returned the expected string.");
}

@end

And finally, run the MyProjTest target in the simulator and it should look like this:

Simple Test Cases with the OCMock
Simple Test Cases with the OCMock

MOBILE APPS TESTING






Advantages and Disadvantages of Automation Testing













Use of add-ins
Cookies in QTP


Mobile Testing with QTP How to install .ipa files for ipad iphone



Test Automation Frameworks Types





No comments:

Post a Comment

Popular Posts