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