【CodeTest】Xcode單元測試基本用法及Quick進一步介紹

學習資料

Xcode單元測試基本用法

  1. 整體測試
    command + u,或者Xcode -> Product -> Test
    ?整體測試.png
  2. 測試標簽欄
    ?測試標簽欄.png
  3. 添加新的測試Target或官方測試類的快捷方式
    ?添加新的測試Target或官方測試類的快捷方式.png
  4. 局部測試快捷方式
局部測試1.png
局部測試2.png

注意:

在用Xcode寫測試類,引用app target中的變量、方法或類時,有時沒有提示.原因是,你剛剛為app target 中的文件添加的變量、方法或類還沒有被編譯器識別,Command + B 編譯一下就好.

Quick進一步介紹

控件測試

控件測試的基本思路是:通過觸發控制器生命周期的相關事件來測試.
我們通過三種方法來觸發:

  1. 訪問控制器的view,這會觸發諸如控制器的.viewDidLoad()
  2. 通過控制器.beginAppearanceTransition()來觸發大部分生命周期事件
  3. 直接調用.viewDidLoad()或者.viewWillAppear()來觸發

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,來觸發控制器的 .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: 觸發.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: 直接調用生命周期事件
                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"))
            })
        }
    }
} 
減少冗余測試文件

如果我們在不同的測試文件中,用到了相同的測試行為,應該考慮用sharedExamples.
比如,不同的類遵循了相同的協議,我們要測試這個協議下不同類的表現.

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介紹到此告一段落,謝謝閱讀!

下載源碼

下載地址

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,252評論 4 61
  • 各位從事iOS以及Mac OS開發的應該都知道,Xcode雖然已經是一個相對很完善的IDE,但是在一些場景下,Xc...
    ac41d8480d04閱讀 1,069評論 3 6
  • 這是優達學城Udacity“數據分析師”課程的“統計學”部分的實踐項目,在這跟大家分享,讓大家了解統計學知識在實驗...
    肖彬_用戶增長_數據分析閱讀 1,832評論 0 4
  • 百悔從前不可追 舉步中年愁煞人 世事豈獨苦這邊 平凡難訴隨浮沉
    善正人生閱讀 231評論 0 1