iOS unit tests under Xcode

Source: Internet
Author: User

XCode has a built-in Ocunit Unit test framework, but the best test framework for the time being is ghunit. With Ghunit + ocmock combination, we can perform more powerful unit testing functions under IOS. This article shows you how to use Ocunit, Ghunit, and Ocmock for unit testing under XCode 4.2.

Ocunit

Create a new Ocunitproject project under XCode, select the Include Unit Tests selection box,


The Ocunit framework automatically adds the Unit Test framework code to US:

XCode automatically generates a Fail test for us in OCUNITPROJECTTESTS.M:

-(void) testexample
{
Stfail (@ "Unit tests is not implemented yet in ocunitprojecttests");
}

Let's run Test to see the effect:

The Red Underline section shows that the test did not pass and meets expectations. We simply write subclasses that inherit from the Sentestcase class like ocunitprojecttests, adding a form such as:-(void) testxxx (); Test function, note that it must be a non-parameter and no return type and that the name is a function prefixed with test.

Ocunit is a bit of official support, and it's better to integrate with XCode.

Ghunit

Ghunit is an open-source unit Testing framework with a visual interface and powerful functionality. Mark wrote an article about Ocunit vs Ghunit, and interested children's shoes can take a look. Ocmock is a unit testing framework that follows the mock object concept written by Mulle Kybernetik for OS X and IOS platforms.

The following describes how to configure Ghunit and Ocmock

1, first, create a single-view application named Ghunitproject, Note: Do not select the Include Unit Tests selection box. Then run, the white screen should appear.

2, add a new test target, select the project name on the left, click on the Add target on the right, add an Empty application application named Tests, and let it be attached to Ghunitproject Note: Do not select the Include Unit Tests the selection box.

3, add the Ghunitios Framework to the Tests project (note that the Tests project). First download the Ghunitios Framework that corresponds to the XCode version. Good English can view the official IOS version of the installation documentation: Click here to skip this 3rd section;

3.1, unzip the Ghunitios frame to Ghunitproject, let Ghunitios.framework and Tests in the same directory.

3.2, go back to XCode, right-click on Frameworks group in project, select Add Files to ... menu, select Ghunitios.framework, note targets to select Tests.

3.3, set Tests Build Settings: Add two flags to the other Linker flag:-OBJC and-all_load.


3.4, delete UTSAppDelegate.h and utsappdelegate.m two files in Tests project;

3.5, modify the MAIN.M in the Tests project as:

#import <UIKit/UIKit.h>
#import <GHUnitIOS/GHUnitIOSAppDelegate.h>

int main (int argc, char *argv[])
{
@autoreleasepool {
Return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Ghunitiosappdelegate class]));
}
}

3.6, choose Compile target Tests>iphone 5.0 Simulator, compile and run, should be able to get the following effect. We haven't written any actual tests yet, so the list is empty.

4, write Ghunit test. Add the Objective C class named Ghunitsampletest to the Tests project. The contents are as follows:
GHUnitSampleTest.h



@interface Ghunitsampletest:ghtestcase
{
}

@end


Ghunitsampletest.m

#import "GHUnitSampleTest.h"

@implementation Ghunitsampletest

-(void) teststrings
{
NSString *string1 = @ "a string";
Ghtestlog (@ "I can log to the Ghunit Test console:%@", string1);

Assert string1 is isn't NULL, with no custom error description
Ghassertnotnull (string1, nil);

Assert equal objects, add custom error description
NSString *string2 = @ "a string";
Ghassertequalobjects (string1, string2, @ "A custom error message. String1 should is equal to:%@.", string2);
}

@end

Then compile and run, click Run, the effect is as follows:

The all column in the diagram shows the test so that the Failed bar shows the tests that were not passed. Strong, Ghunit. You can add new tests to ghunitsampletest, such as:

-(void) Testsimplefail
{
Ghasserttrue (NO, nil);
}

We can add more test classes to Tests, as long as the class is inherited from Ghtestcase, and the test method in it is no parameter, no return value, and the method name is prefixed with test.

Ocmock

Let's add Ocmock.

1, we can only add Ocmock in a static library. Create a new Libraries directory under the Ghunittest directory that is peer to the Tests directory. Download the static library file and extract the header files to the directory.
File Download: Header file libocmock.a, Framework file: Ocmock framework, open downloaded OCMOCK-1.77.DMG, copy the ' Release/library/headers/ocmock ' directory to Under the Libraries. The final directory structure is as follows:

2, create a new group named Libraries in the Ghunittest project, import libocmock.a and directory Ocmock, and note that target is Tests.

3, set the Build Setting for Tests. Let Libray Search Paths contain $ (srcroot)/libraries:

Add $ (srcroot)/libraries in Header Search Paths, and select the Recursive selection box.

4, write Ocmock test. Add the Objective C class named Ocmocksampletest to the Tests project. The contents are as follows:
OCMockSampleTest.h

#import <GHUnitIOS/GHUnit.h>

@interface Ocmocksampletest:ghtestcase

@end

Ocmocksampletest.m

 #import "OCMockSampleTest.h" 
#import <ocmock/ocmock.h>

@implementation ocmocksampletest

//Simple test to ensure building, linking,
//and running test case works in the project
-(void) Testocmockpas S
{
id mock = [ocmockobject mockForClass:NSString.class];
[[[Mock Stub] andreturn:@ "Mocktest"] lowercasestring];

NSString *returnvalue = [mock lowercasestring];
Ghassertequalobjects (@ "Mocktest", returnvalue,
@ "should has 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 has returned the E xpected string. ");
}

@end

Compile and run, click Run, the effect is like.

This concludes the introduction of the Ocunit,ghunit,ocmock Unit test under IOS. There are, of course, other test frameworks, such as the GTM that Google produced.

iOS unit tests under Xcode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.