[英]Testing Dark Mode using snapshot testing
Any leads how can we use Snapshot testing to test darkmode implementation?任何线索我们如何使用快照测试来测试暗模式实现? Or any other testing strategy for dark mode on iOS.
或 iOS 上暗模式的任何其他测试策略。
When XCUITest is one of the options along with XCTest (unit-test).当 XCUITest 是 XCTest (单元测试)的选项之一。 The problem with UITest in dark mode means for every app flow it needs to be run twice.
UITest 在黑暗模式下的问题意味着对于每个应用程序流,它都需要运行两次。
Will Unittest cases be enough to suffice testing needs for dark mode? Unittest 用例是否足以满足暗模式的测试需求? Can you think of any use case which won't be covered using only unit testing for dark mode?
您能想到仅使用暗模式的单元测试无法涵盖的任何用例吗?
I use FBSnapshotTestCase to get light & dark snapshots in a unit test target:我使用FBSnapshotTestCase在单元测试目标中获取明暗快照:
final class ViewControllerSnapshotTests: FBSnapshotTestCase {
private var sut: ViewController!
override func setUp() {
super.setUp()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
sut = storyboard.instantiateViewController(
identifier: String(describing: ViewController.self)
)
usesDrawViewHierarchyInRect = true
recordMode = false // set to true to record snapshots
}
override func tearDown() {
sut = nil
super.tearDown()
}
func test_light() {
sut.overrideUserInterfaceStyle = .light
FBSnapshotVerifyViewController(sut)
}
func test_dark() {
sut.overrideUserInterfaceStyle = .dark
FBSnapshotVerifyViewController(sut)
}
}
This produced the following snapshots:这产生了以下快照:
The tricks were:诀窍是:
usesDrawViewHierarchyInRect = true
on the snapshot test caseusesDrawViewHierarchyInRect = true
overrideUserInterfaceStyle
on the view controlleroverrideUserInterfaceStyle
The usual advantages of snapshot tests over UITests apply.快照测试相对于 UITests 的通常优势适用。 Snapshots are slower than normal unit tests, but much faster than UITests because we don't have to navigate through the app to reach each view controller.
快照比普通单元测试慢,但比 UITests快得多,因为我们不必浏览应用程序即可到达每个视图 controller。
How fast?多快? Here's my timing, running on a 2015 MacBook Pro.
这是我在 2015 MacBook Pro 上运行的时间。
Test Suite 'ViewControllerSnapshotTests' started at 2021-04-20 21:35:26.856
Test Case '-[SOTests.ViewControllerSnapshotTests test_dark]' started.
Test Case '-[SOTests.ViewControllerSnapshotTests test_dark]' passed (0.101 seconds).
Test Case '-[SOTests.ViewControllerSnapshotTests test_light]' started.
Test Case '-[SOTests.ViewControllerSnapshotTests test_light]' passed (0.029 seconds).
Test Suite 'ViewControllerSnapshotTests' passed at 2021-04-20 21:35:26.987.
Executed 2 tests, with 0 failures (0 unexpected) in 0.130 (0.131) seconds
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.