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