Unit testing in Swift

Source: Internet
Author: User

The class to test

Import Cocoaclass simpleinterest:nsobject{    func calculate (loanamount:double, var interestrate:double,years:int) ->double    {        interestrate = interestrate/100.0 let        interest = Double (years) * InterestRate * LOANAMOUNT
   return Loanamount + Interest    }        func Calculateadd (num1:double,num2:double)->double    {        return NUM1 + num2    }}

  

Test code:

Import xctestimport cocoa@testable Import testdemoclass testdemotests:xctestcase {var mysimpleinterestcaculator: Simpleinterest = Simpleinterest () override Func SetUp () {Super.setup () print ("Starting t                EST ") Print (" This method is called before using each test method in this class, you can insert the corresponding global setting code here ")} override func TearDown () {            Print ("End Test") print ("This method is called after using each test method in this class, where you can insert global configuration code-such as cleanup settings, etc.") Super.teardown ()} Func Testadd () {Let result = Mysimpleinterestcaculator.calculateadd (NUM2:12)//accuracy represents fine Accuracy xctassertequalwithaccuracy (result, 24.01, accuracy:0.1, "error:unexpected result->\ (Result)")} F        UNC Testsimpleinterest () {Let result = Mysimpleinterestcaculator.calculate (25_000, interestrate:0.08, Years:10) Xctassertequalwithaccuracy (result, 25200, accuracy:0.1, "error:unexpected result->\ (Result)")}}

The test button on the left side of the stand-alone test!

Tests that are always running:

Unit tests must run to work if you want to run the tests every time you run the application?

Click Project--> Target--Build phases----Target Dependencies:add dependency

At this point, the test is specified as a dependency of the application, so the test is compiled and executed the first time the application is compiled! If a test does not pass, the application will not be compiled

Unit testing in Swift

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.