關(guān)于 iOSSnapshotTestCase / FBSnapshotTestCase 使用的一點備注



FBSnapshotTestCase 現(xiàn)在是由Uber 維護了。>>> Github 地址:iOSSnapshotTestCase


新建項目時候勾選 tests target ?
image.png
如果新建項目時候忘記勾選 tests target,按照下面方式新建一個:File >> New >> Target >> Unit Testing Bundle
截屏2020-04-25 11.08.41.png
image.png
導(dǎo)入 iOSSnapshotTestCase


target 'TestDemo' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!
  
  target 'TestDemoTests' do
    inherit! :search_paths
    pod 'iOSSnapshotTestCase'
  end

end


iOSSnapshotTestCase 環(huán)境配置
Name Value
FB_REFERENCE_IMAGE_DIR $(SOURCE_ROOT)/$(PROJECT_NAME)Tests/ReferenceImages
IMAGE_DIFF_DIR $(SOURCE_ROOT)/$(PROJECT_NAME)Tests/FailureDiffs


在如下位置配置參數(shù):
image.png

Tests code:

import XCTest
import FBSnapshotTestCase
@testable import TestDemo

class TestDemoTests: FBSnapshotTestCase {
    
    var vc: UIViewController!

    override func setUp() {
        super.setUp()
        vc = SecondViewController()
        self.recordMode = true
    }

    override func tearDown() {
        vc = nil
        super.tearDown()
    }

    func testExample() {
        FBSnapshotVerifyView(vc.view)
    }
}
第一次 Run 的時候需要把 self.recordMode 設(shè)置為 true. 保存作為參照的圖,第一次run test 是 failed 的,原因也提示在下圖了。第二次 run 就可以把 self.recordMode 設(shè)置為 false.
image.png
image.png
?踩的坑:

Run tests 的時候遇到 error:


image.png
原因是在create target的時候選中了UI Testing Bundle 而不是 Unit Testing Bundle.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容