學(xué)習(xí)資料
Xcode單元測試基本用法
- 整體測試
command + u,或者Xcode -> Product -> Test
- 測試標(biāo)簽欄
- 添加新的測試Target或官方測試類的快捷方式
- 局部測試快捷方式
注意
:
在用Xcode寫測試類,引用app target中的變量敷待、方法或類時,有時沒有提示.原因是,你剛剛為app target 中的文件添加的變量、方法或類還沒有被編譯器識別,Command + B 編譯一下就好.
Quick進(jìn)一步介紹
控件測試
控件測試的基本思路是:通過觸發(fā)控制器生命周期的相關(guān)事件來測試.
我們通過三種方法來觸發(fā):
- 訪問控制器的view,這會觸發(fā)諸如控制器的.viewDidLoad()
- 通過控制器.beginAppearanceTransition()來觸發(fā)大部分生命周期事件
- 直接調(diào)用.viewDidLoad()或者.viewWillAppear()來觸發(fā)
ViewController.swift
import UIKit
public class ViewController: UIViewController {
public var bananaCountLabel : UILabel!
public var button : UIButton!
override public func viewDidLoad() {
super.viewDidLoad()
bananaCountLabel = UILabel(frame: CGRect(x: 100, y: 100, width: 100, height: 40))
view.addSubview(bananaCountLabel!)
bananaCountLabel.text = "0"
bananaCountLabel.backgroundColor = UIColor.orangeColor()
button = UIButton(type: .Custom)
view.addSubview(button)
button.frame = CGRect(x: 100, y: 200, width: 100, height: 40)
button.backgroundColor = UIColor.blackColor()
button.setTitle("Button", forState: .Normal)
button.addTarget(self, action: "buttonAction", forControlEvents: .TouchUpInside)
}
func buttonAction() {
let bananaCount = Int(bananaCountLabel.text!)
bananaCountLabel.text = String(bananaCount! + 1)
}
override public func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
ViewControllerSpec.swift
import Quick
import Nimble
import UseQuick
class BananaViewControllerSpec: QuickSpec {
override func spec() {
var viewController : ViewController!
beforeEach { () -> Void in
viewController = ViewController()
// // storyboard初始化
// let storyboard = UIStoryboard(name: "main", bundle: nil)
// viewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
}
// #1
describe(".viewDidLoad()", { () -> Void in
beforeEach({ () -> () in
// 方法1: 訪問控制器的view,來觸發(fā)控制器的 .viewDidLoad()
let _ = viewController.view
})
it("sets banana count label to zero", closure: { () -> () in
print(viewController.bananaCountLabel.text)
expect(viewController.bananaCountLabel.text).to(equal("0"))
})
})
// #2
describe("the view", { () -> Void in
beforeEach({ () -> Void in
// 方法2: 觸發(fā).viewDidLoad(), .viewWillAppear(), 和 .viewDidAppear() 事件
viewController.beginAppearanceTransition(true, animated: false)
viewController.endAppearanceTransition()
})
it("sets banana count label to zero", closure: { () -> () in
expect(viewController.bananaCountLabel.text).to(equal("10"))
})
})
// #3
describe(".viewDidLoad()", { () -> Void in
beforeEach({ () -> () in
// 方法3: 直接調(diào)用生命周期事件
viewController.viewDidLoad()
})
it("sets banana count label to zero", closure: { () -> () in
expect(viewController.bananaCountLabel.text).to(equal("10"))
})
})
// 測試UIControl事件
describe("the more banana button") { () -> () in
beforeEach({ () -> Void in
viewController.viewDidLoad()
})
it("increments the banana count label when tapped", closure: { () -> () in
viewController.button.sendActionsForControlEvents(.TouchUpInside)
expect(viewController.bananaCountLabel.text).to(equal("1"))
})
}
}
}
減少冗余測試文件
如果我們在不同的測試文件中,用到了相同的測試行為,應(yīng)該考慮用sharedExamples
.
比如,不同的類遵循了相同的協(xié)議,我們要測試這個協(xié)議下不同類的表現(xiàn).
Dolphin.swift
public struct Click {
public var isLoud = true
public var hasHighFrequency = true
public func count() -> Int {
return 1
}
}
public class Dolphin {
public var isFriendly = true
public var isSmart = true
public var isHappy = false
public init() {
}
public init(happy: Bool) {
isHappy = happy
}
public func click() -> Click {
return Click()
}
public func eat(food: AnyObject) {
isHappy = true
}
}
Mackerel.swift
public class Mackerel {
public init() {
}
}
Cod.swift
public class Cod {
public init() {
}
}
EdibleSharedExamplesConfiguration.swift
import Quick
import Nimble
import UseQuick
class EdibleSharedExamplesConfiguration: QuickConfiguration {
override class func configure(configuration: Configuration) {
sharedExamples("something edible") { (sharedExampleContext : SharedExampleContext) -> Void in
it("makes dolphins happy", closure: { () -> () in
let dolphin = Dolphin(happy: false)
let edible = sharedExampleContext()["edible"]
dolphin.eat(edible!)
expect(dolphin.isHappy).to(beFalsy())
})
}
}
}
MackerelSpec.swift
import Quick
import Nimble
import UseQuick
class MackerelSpec: QuickSpec {
override func spec() {
var mackerel : Mackerel!
beforeEach { () -> () in
mackerel = Mackerel()
}
itBehavesLike("something edible") { () -> (NSDictionary) in
["edible" : mackerel]
}
}
}
CodSpec.swift
import Quick
import Nimble
import UseQuick
class CodSpec: QuickSpec {
override func spec() {
var cod : Cod!
beforeEach { () -> () in
cod = Cod()
}
itBehavesLike("something edible") { () -> (NSDictionary) in
["edible" : cod]
}
}
}
Quick介紹到此告一段落,謝謝閱讀!