IOS unit test in xcode

Source: Internet
Author: User
IOS unit test in xcodeLuo chaohui (http://blog.csdn.net/kesalin) CC license, reprinted please indicate the source

Xcode has a built-in ocunit unit testing framework, but the best testing framework is ghunit. Through the ghunit + ocmock combination, we can perform more powerful unit test functions in IOS. This article demonstrates how to use ocunit, ghunit, and ocmock for unit testing in xcode 4.2.

Ocunit

Create an ocunitproject project under xcode, and select the include unit tests selection box,

Ocunit framework will automatically add the unit test framework code for us:

Xcode automatically generates a fail test for us in ocunitprojecttests. M:

- (void)testExample{    STFail(@"Unit tests are not implemented yet in OCUnitProjectTests");}

Let's run test to see the effect:

The red underline section shows that the test failed, as expected. We only need to write a subclass inherited from the sentestcase class like ocunitprojecttests, and add the test function in the form of-(void) testxxx, note that it must be a function with no parameter, no return type, and the name is prefixed with test.

Ocunit is somewhat officially supported, which is better than xcode integration.

Ghunit

Ghunit is an open-source unit testing framework with visual interfaces and powerful functions. Mark wrote an article
Ocunit vs ghunit article. If you are interested, you can take a look at it.
Ocmock is written by mulle kybernetik for OS X and IOS.
The unit test framework of the mock Object Concept.

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, a white screen is displayed.

2. Add a new test target, select the project name on the left, and click Add target on the right to add an empty application named tests, which is attached to the ghunitproject. Note: do not select the include unit tests selection box.

3. Add the ghunitios framework to the tests Project (note the tests project. Download the ghunitios framework corresponding to the xcode version. For good English, you can directly view the official IOS version of the installation documentation: click here to view, skip this section
3; otherwise, see.

3.1. decompress the ghunitios framework to the ghunitproject so that the ghunitios. Framework and tests are in the same directory.

3.2. Go back to xcode, right-click frameworks group in the project, select add files to..., and select ghunitios. Framework. Note that select tests for targets.

3.3. Set the build settings of tests: Add two flags in other linker flags:-objc and-all_load.

3.1. Delete the utsappdelegate. h and utsappdelegate. M files in the tests project;

3.2. modify main. m in the tests project:

#import <UIKit/UIKit.h>#import <GHUnitIOS/GHUnitIOSAppDelegate.h>int main(int argc, char *argv[]){    @autoreleasepool {        return UIApplicationMain(argc, argv, nil, NSStringFromClass([GHUnitIOSAppDelegate class]));    }}

3.3. Select the compilation target tests> iPhone 5.0 simulator to compile and run it. The following results should be obtained. Currently, we have not compiled any actual tests, so the list is empty.

4. Compile a ghunit test. Add the Objective C class named ghunitsampletest to the tests project. The content is as follows:
Ghunitsampletest. h

#import <GHUnitIOS/GHUnit.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 not 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 be equal to: %@.", string2);}@end

Then compile and run and click Run. The effect is as follows:

The "all" column in the figure shows the test, and the "failed" column shows the test that failed. Powerful, ghunit. You can add a new test to ghunitsampletest, for example:

- (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 methods in it all have no parameters, no return value, and the method name is prefixed with test.

Ocmock

Next we will add ocmock.

1. We can only add ocmock as a static database. Create the libraries directory under the ghunittest Directory, which is the same as the tests directory. Download the static library file and decompress the header file to this directory.
File Download: header file libocmock. A, framework file: ocmock framework, open the downloaded ocmock-1.77.dmg, copy the 'release/library/headers/ocmock'
Directory to libraries. The final directory structure is as follows:

2. Create a group named libraries in the ghunittest project and import libocmock. A and the ocmock directory. Note that the target is tests.

3. Set the build setting of tests. Let libray search paths include $ (srcroot)/Libraries:

Add $ (srcroot)/libraries to header search paths and select the recursive selection box.

4. Write ocmock tests. Add the Objective C class named ocmocksampletest to the tests project. The content is 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)testOCMockPass{    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

Compile and run and click Run. The result is as follows.

Now, the introduction to ocunit, ghunit, and ocmock unit tests in IOS is over. Of course, there are other testing frameworks, such as those produced by Google.
GTM.

References:
Ocmock: http://ocmock.org/
Unit testing in xcode 4-use ocunit and sentest instead of ghunit
Ghunit reference: http://gabriel.github.com/gh-unit/

Related Article

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.