IOS7 First Experience (2)--unit test

Source: Internet
Author: User

In Xcode 4.6 and previous versions, it has been felt that unit testing this part of the function is very chicken, used to feel very awkward. This time Xcode 5.0 introduces unit tests by default, so take a look at what's going on with the previous version! ~_~

1. First open the previous section of the project, of course, you can also create a new blank project;

2. First look at the navigation area of the toolbar, compared to the 4.6 changes, as shown in:

It's not hard to find a button that shows the test Navigator (show Testing navigator) more than Xcode 4.6.

3. Press Command+u to run the unit test ... After a few seconds, you can see a hint similar to 4.6 in the Issue Navigator (Problem Navigator) , as shown in:

4. Click the show the test Navigator button, as shown in:

5. In the helloworldtests.m file, add the following code before @end:

-(void) testExample2

{

Xctasserttrue (1 = = 1, @ "just a Test");

}

6. Press Command+u to run the unit test ... After a few seconds, you can see the following test results in test Navigator :

7. Right-click on the testExample2 in the navigation area to display the secondary menu as shown in:

You can see in the diagram that a unit test ~_~ can be run/disabled individually.

Note : If you choose Test "TestExample2" is not difficult to find, in fact, Testexample and testExample2 two unit tests will run, and will be in the problem navigation area All test results are displayed in the test navigation area , but only the test state of TestExample2 is updated. This issue is believed to be improved in the future version of Xcode. :]

8. The following shows how to test the data model, starting with a new class named Datamodel in the HelloWorld project, inheriting from NSObject;

9. First right-click on the HelloWorld node in the navigation area, then select New Filein the secondary menu ..., select objective-c classin the pop-up dialog box, then click the Next button:

10. In the next dialog box , type:Datamodel, and confirm subclassof: Select nsobject, then click Next button:

11. Click and open DataModel.h, add a property definition, and the adjusted code is as follows:

#import <Foundation/Foundation.h>

@interface Datamodel:nsobject

@property (Assign, nonatomic) Nsinteger myId;

@end

12. Click and open DATAMODEL.M, add the Init method, set the default value of myID to 100, and the adjusted code is as follows:

#import "DataModel.h"

@implementation Datamodel

-(ID) init

{

if (self = [super init])) {

_myid = 100;

}

return self;

}

@end

13. Click the show the test Navigator button in the Navigation Area toolbar, then right-click Helloworldtests and select New test Class in the pop-up menu ... :

14. In the next dialog box , type:datamodeltests, and confirm subclassof: Select xctestcase, and then click Next button and click the Create button directly in the next dialog box:

15. Click on the Datamodeltests node, first introduce the Datamodel header file:

#import "DataModel.h"

16. Remove the Testexample method, and then add the following code before @end:

-(void) Testmodeldefaultid

{

Datamodel *model = [[Datamodel alloc]init];

Xctassertequals (Model.myid, @ "The default ID should be 100");

}

17. Press Command+u to run the unit test ...

Get it done!

For the benefits of unit testing, this article does not go into the ha, I believe there are Java or. NET development experience of friends are deeply touched. To learn more about unit testing in iOS7, you can refer to Apple's official documentation.

Copyright notice: This article by http://blog.csdn.net/liufan321 or HTTP://WWW.CNBLOGS.COM/LIUFAN9 original, welcome reprint share. Please respect the author Labor, reproduced when the statement and the author of the blog link, thank you!

IOS7 First Experience (2)--unit test

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.