Beginner play Test-Driven Development(TDD) in swift
2 day ago i learn about TDD: Test Driven Development when i read content i think why we should have TDD process when we develop some app. I found it from 3 site below.
- http://www.andrewcbancroft.com/2014/12/16/tdd-ios-swift-whats-goal
- http://nshipster.com/xctestcase/
- http://pawanpoudel.svbtle.com/test-driven-development-in-swift
Two goal for doing Test-Driven Development
Make sure my code is in the right place Make sure my logic is correct
I try coding abit on “Nil Test”, use XCTAssertNotNil to assert the existence of a given value.
Let try…
1 2 3 4 5 6 7 8 9 10 |
tdd_exTests.swift func testSetupVarFunctionAfterViewDidLoads() { let vc = ViewController() vc.viewDidLoad() XCTAssertNotNil((vc.myVar), "myVar is not setup") } |
and .. my view controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
viewcontroller.swift class ViewController: UIViewController { var myVar:String? override func viewDidLoad() { super.viewDidLoad() // self.myVar = setupVar("My Text") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func setupVar(text:String) -> String { return text } } |
Try try try … and don’t forget check your test in Target Membership on File inspector
and then run test use command+u …
WTH!!! We will see
Oh why.. why it not pass , because myVar on view controller is Nil
You try to uncomment this line on view controller and run test again (command+u)
1 2 3 |
self.myVar = setupVar("My Text") |
Success!! We will see
Wow nice development process for us. You can try other XCTest Assertions on list
- Fundamental Test : XCTAssert(expression, format…)
- Boolean Tests : XCTAssert[False]True(expression, format…)
- Equality Tests : XCTAssert[Not]Equal(expression1, expression2, format…)
- Equality(Double,Float or other floating-point) Tests : XCTAssert[Not]EqualWithAccuracy(expression1, expression2, accuracy, format…)
- Nil Tests : XCTAssert[Not]Nil(expression, format…)
- Unconditional Failure : XCTFail(format…)
Example project : Download
Next time we will talk about Performance Testing and XCTestExpectation.
K Bye .
Thank nice image from
http://littlevisuals.co/