Ios7 initial experience (2) -- unit test

Source: Internet
Author: User

In xcode 4.6 and earlier versions, I always felt that the unit test function was very poor and it was quite awkward to use. This time xcode 5.0 introduces unit testing by default. Let's take a look at the improvements compared with previous versions !~ _~

1. First open the project used in the previous example. Of course, you can also create a blank project;

2. First check the toolbar of the navigation area. What are the changes compared with 4.6, as shown in:

It is not hard to find that there is one moreShow the test Navigator(Display Test Navigator);

3. PressCommand + uRun unit test ...... After a few seconds, you canIssue Navigator(Question Navigator)See a message similar to 4.6, as shown in:

4. ClickShow the test Navigator(Display Test Navigator)As shown in:

5. InHelloworldtests. mFile,@ EndAdd the followingCode:

-(Void) Testexample2 {xctasserttrue (1=1,@"Just a test");}

6. PressCommand + uRun unit test ...... After a few seconds, you canTest NavigatorSee the following test results:

7. In the navigation areaTestexample2Right-click, and the secondary menu is displayed, as shown in:

In the figure, you can see thatRun/DisableA unit test ~ _~.

Remarks: If you selectTest "testexample2"It's not hard to find that, in factTestexampleAndTestexample2Both unit tests run andProblem navigation areaAll test results are displayed inTest navigation areaBut only updateTestexample2. This problem is expected to be improved in the future xcode version. :]

8. The following shows how to test the data model.HelloworldCreate a new project namedDatamodelClass, inherited fromNsobject;

9. FirstHelloworldRight-click the node and selectNew file..., SelectObjective-C ClassAnd then clickNextButton:

10. In the next dialog boxClassInput:DatamodelAnd confirmSubclass: SelectNsobjectAnd then clickNextButton:

11. Click and openDatamodel. hAdd an attribute definition. The adjusted code is as follows:

 
# Import<Foundation/Foundation. h>@ InterfaceDatamodel: nsobject @ property (assign, nonatomic) nsinteger myid;@ End

12. Click and openDatamodel. m, AddInitMethod, SetMyidThe default value is100The adjusted code is as follows:

 
# Import "Datamodel. h"@ ImplementationDatamodel-(ID) Init {If(Self =[Super init]) {_ myid=100;}ReturnSelf ;}@ End

13. ClickShow the test Navigator(Display Test Navigator)And right-clickHelloworldtestsIn the pop-up menu, selectNew test class...:

14. In the next dialog boxClassInput:DatamodeltestsAnd confirmSubclass: SelectXctestcaseAnd then clickNextButton, and directly click in the next dialog boxCreateButton:

15. ClickDatamodeltestsNode, first introduceDatamodelHeader file:

 
# Import "Datamodel. h"

16. DeleteTestexampleMethod, and then@ EndAdd the following code before:

-(Void) Testmodeldefaultid {datamodel* Model =[[Datamodel alloc] init]; xctassertequals (model. myid,100,@"The default ID shocould be 100");}

17. PressCommand + uRun unit test ......

 

Work done!

This article will not go into details about the benefits of unit testing. I believe all of my friends who have experience in Java or. NET development are deeply touched. For more information about unit testing in ios7, see the official documentation of apple.

Copyright statement: This article consistsHttp://blog.csdn.net/liufan321OrHttp://www.cnblogs.com/liufan9Original. You are welcome to share this article. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!

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.