简体   繁体   English

使用快照测试测试暗模式

[英]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:这产生了以下快照:

test_dark@2x.png 和 test_light@2x.png

The tricks were:诀窍是:

  • Set usesDrawViewHierarchyInRect = true on the snapshot test case在快照测试用例上设置usesDrawViewHierarchyInRect = true
  • Set overrideUserInterfaceStyle on the view controller在视图 controller 上设置overrideUserInterfaceStyle

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.

相关问题 IOS UI测试:如何使用fastlane快照通过代码解除一系列系统警报(麦克风访问) - IOS UI Testing: how to dismiss a series of system alerts( microphone access ) via code using fastlane snapshot 如何在 swift UITests 的 XCUIApplication 中设置暗模式? - How to set Dark Mode in XCUIApplication in swift UITests? 在真实设备上的 XCUI 测试中检测暗模式 - Detect dark mode in XCUI test on real devices 使用XCUITest设置UI自动化测试的可访问性标识符 - Setting accessibility identifier for UI automation testing using XCUITest Swift 中的异步 UI 测试 - Asynchronous UI testing in Swift iOS 14 / UI 测试 DatePicker - iOS 14 / UI Testing DatePicker 在 UiTests 或 UnitTests 中测试 UserDefaults 值 - Testing UserDefaults values in UiTests or UnitTests 使用并行测试一次运行所有测试时,输入文本字段很不稳定 - Inputting textfields is flaky when running all tests at once using parallel testing AppCenter 设备测试 - 测试用例方法失败 - AppCenter Device Testing - Test Case Method Failure XCUITest使用Springboard测试本机Callkit UI。 如何获得来电者标签? - XCUITest Testing Native Callkit UI Using Springboard. How to get the caller label?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM