Introduction and comparison of iOS unit test--xctest and ghunit frameworks under Xcode6

Source: Internet
Author: User
Tags uikit

iOS unit Test (unittest)

Testing in iOS allows you to choose between Xctest and Ghunit to test both frameworks. Xctest is integrated in Xcode, while Ghunit is a well-known open source framework.

The so-called unit test, refers to the program is divided into a number of small units, each small unit test. The so-called small unit can be a method, a UI control, a simple event, and so on. This article does not focus on the meaning of testing, so choose the simplest addition function, which describes how iOS is unit tested.

Xctest Framework Xctest Introduction

Xctest, formerly known as Ocunit, is a test framework integrated in Xcode. It is relatively simple to use, and now the XCODE6 default is built with Xctest. Of course, if the old project does not have this test, or accidentally deleted it does not matter, you can re-add the following.

First, the default test name is the "project name Tests" format. For example, we set up a project called "iOS Practice", the default test name is "ios practice Tests".

The next step is to start building your own unit tests from scratch.

Xctest Configuration

First, at the bottom left of the project settings, select Add a target:

Then select the Cocoa Touch testing Bundle within the other list.

Select Finish to create a new target. After a successful addition, you can see that a new IOS__TEST.M file has been added and a new target. In general, the system will automatically add a scheme, if not, can also be in the upper left corner of the Run/Stop button next, click on a scheme, select New scheme, and select the corresponding target.

After these configurations have been done, this should be the case:

The three numbers in the figure are the ones that should be changed.

Using Xctest

After the basic configuration is done, you can start writing code and using Xctest. First analyze the. m files that the system created for us:

#import <UIKit/UIKit.h>#import <XCTest/XCTest.h>@interfaceIos___tests:xctestcase@end@implementationios___tests-(void) SetUp {[SuperSETUP];//Put Setup code here. This method isCalled before the invocation ofEach test methodinchThe class.}- (void) TearDown {//Put teardown code here. This method isCalled after the invocation ofEach test methodinchThe class.[SuperTearDown];} - (void) Testexample {//This isAn example ofA functional test Case. Xctassert (YES, @"Pass");}@end

Unlike writing iOS, the test file does not have. h because it does not need to be so complicated, the declaration of the class is done together in the @interface of the. m file. For example, here we create a ios___tests class that inherits from Xctestcase, and all of the unit test classes must inherit from this base class. When the system performs unit tests, it searches all xctestcase subclasses, and executes the methods that begin with test. At this point, each method that starts with test is a test case (TestCase). For example, the Testexample method here will be executed as a test case.

The setup and Teardown methods are executed at the beginning and end of each test case. This can simplify a lot of code. For example, I need to test some methods of operating the database, it is necessary to connect the database in the Setup method and build the table, delete the table in the Teardown method and close the connection to the database.

Next, add a simple method that needs to be tested in the Viewcontroller and declare it inside the. h file:

//ViewController.h- (int)addNumber:(int)a WithNumber:(int)b;//ViewController.m- (int)addNumber:(int)a WithNumber:(int)b {    return a + b;}

You can then start testing the method in the test class.

//ios___tests.m -(void ) testaddmethodright{Viewcontroller *VC = [[Viewcontroller alloc] init]; int  answer = [VC addnumber:1  Withnumber:2 ];    //answer = 3  Xctassertequal (3 , answer);}    -(void ) testaddmethodwrong{Viewcontroller *VC = [[Viewcontroller alloc] init];    int  wronganswer = 0 ; int  answer = [VC addnumber:1  Withnumber:2 ];    //answer = 3  Xctassertequal (Wronganswer, answer);}  

The xctassertequal here is an assertion macro, which simply means that if its two parameter values are not equal, it will be an error. This is where unit testing is intended. Obviously the second Test failed.

Next, run the target and note that it is run with the COMMAND + U shortcut key. You can then see the following running results:

Xcode helps us to mark the passing (green tick) and the failed test (Red fork). The debug area also outputs very detailed debugging information, including the cause of each test failure, the time of the test run, and so on.

The above is the simple use of the Xctest test framework.

Ghunit Framework Ghunit Introduction

Ghunit is a very well-known open source framework on GitHub, but because of the need for manual configuration, it's a little more cumbersome than the xctest built into Xcode, but it has a GUI, more straightforward, and more powerful.

Configure Ghunit

First go to https://github.com/gh-unit/gh-unit/downloads to download the library file, after decompression can get a framework called "ghunitios.framework". This framework needs to be used later.

Next create a new target, note that the target type is a normal "single application View", named "Ghunittest". Note that because Xcode thinks this is an ordinary application, it will also create a corresponding xctest for him, because we do not need to, so directly delete the target and the corresponding file can be. Then add the Ghunitios.framework file that you just downloaded and extracted to the target under Ghunittest. The configured interface is as follows:

You can see that the icon for Ghunittest is consistent with the icon for iOS practice, because they are all "single application View."

Then we need to make some changes to this common application so that it is not normal and becomes a magical unit test application. First remove the appdelegate file, delete the. h and. m files, and then change the main.m file under supporting files to this:

#import <UIKit/UIKit.h>#import <GHUnitIOS/GHUnitIOSAppDelegate.h>  //修改引用文件int main(intchar * argv[]) {    @autoreleasepool {        returnclass]));// 这里改一下类名    }}

Here we no longer use Apple's own appdelegate, but instead use the ghunitiosappdelegate provided by the Ghunit framework. In order to avoid conflicts with the main program, we renamed Ghunittest the target inside the Viewcontroller to Ghuviewcontroller.

At this point, the configuration of the Ghunit framework is complete, slightly more complex than the xctest. The next step is to write the code and use the framework.

Using Ghunit

As with the Xctest framework, only. M is required and the class is declared in the @interface of the. m file. Create a new empty OC file named "MYGHUNITTESTCASE.M". Before testing, you need to add the tested class to ghunittest, the target's compile sources.

Next, declare a class named Myghunittestcase, and inherit from Ghtestcase. The complete code is as follows:

#import <Foundation/Foundation.h> #import "ViewController.h" #import <GHUnitIOS/GHTestCase.h>  @interface myghunittestcase : ghtestcase{}@end @implementation myghunittestcase - (void) setup{}-(void) teardown{}-(void) testaddmethodright{Viewcontroller *VC = [[Viewcontroller alloc] init];intAnswer = [VC Addnumber:1Withnumber:2];//answer = 3Ghassertequals (Answer,3, @"The value of answer should be 3");} - (void) testaddmethodwrong{Viewcontroller *VC = [[Viewcontroller alloc] init];intWronganswer =0;intAnswer = [VC Addnumber:1Withnumber:2];//answer = 3Ghassertequals (answer, Wronganswer, @"The value of answer should not be 0");}@end

The test code for this class is almost identical to the previous introduction of Xctest, so it's not too much to introduce. Command + R run a bit.

Ghunit GUI do is still very good, for developers debugging problems, but it seems not fit IOS8 ....

Click the Run button in the top right corner to show the success or failure of each test. Click on the failed test to see the detailed method call stack.


Summarize

As you can see, Xctest's advantage lies in its quick and easy configuration, while Ghunit's advantage is that it is more powerful, so it is recommended to consider the Ghunit test framework when doing larger projects.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Introduction and comparison of iOS unit test--xctest and ghunit frameworks under Xcode6

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.