Unit testing of app development with Swift language

Source: Internet
Author: User

As a quality assurance application, of course, unit testing, as well as the Swift-developed app, this article will introduce a simple example of unit testing in Swift.

Here we use the Xctest framework of the Xcode template, which contains a class named Xctestcase, all of the test classes should inherit from it, according to the conventional test method names should start with test, and can not contain any parameters, only then, These test methods can be executed automatically when the test is run, and in each test method, we can invoke the xctassert* function to assert the success of an operation, such as the judgment of the function xctassertequal, greater than the function Xctassertgreaterthan, etc.

Don't say much, first create an iOS single View application project with Xcode and choose Swift as your development language.

You can see that the directory structure after the creation is complete should be so (here my project is named Testdemo):

The default template will create two folders, one with the name of the project (here is Testdemo), the main program for placing the app, and the other is "project name +tests" (Here is testdemotests) for placing the test code;

It is important to note that the class you are testing needs to be used in test, so you need to select both Testdemo and testdemotests in targets when creating the class file;

As I add a class named URL in the main application:

When the creation is complete, enter the code:

1 class Url {2 Let baseurl:string3     4 Init (baseurl:string) {5Self.baseurl =BASEURL6     }7     8Func Getactualpathfrom (resourcepath:string, Segments: [String:string]),String {9         varActualpath =ResourcePathTen          for(Key,value)inchSegments { One             varSegmentplaceholder = "{\ (key)}" AActualpath =actualpath.stringbyreplacingoccurrencesofstring (Segmentplaceholder, Withstring:value, Options:. Literalsearch, Range:nil) -         } -         returnBASEURL +Actualpath the     } -}

Then create the urltests file under the Testdemotests folder, at which point the class only needs to be used in the test, so you only need to select Testdemotests in targets:

Once created, import the Xctest framework to enter the test code:

Import Xctestclass urltests:xctestcase {varUrlinstance = URL (baseUrl: "http://localhost:8080/api/") func testshouldgetcorrectpathwhennosegmentprovided () {Let ResourcePath= "Customers"Let result=Urlinstance.getactualpathfrom (ResourcePath, Segments: [String:string] ()) xctassertequal (result,"Http://localhost:8080/api/customers", "Can not get corrent path when no segments provided")} func testgetcorrectpathgivenonesegment () {Let ResourcePath= "Customer/{id}"Let result= Urlinstance.getactualpathfrom (ResourcePath, Segments: ["id": "10"]); Xctassertequal (Result,"Http://localhost:8080/api/customer/10", "Can not get corrent path if only one segment provided")    }}

Final Command+u Execution test

Unit testing of app development with Swift language

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.