我们的Swift用户界面快照测试在本地通过,但在持续集成环境中失败。以下是实际的修复方案。

发布日期:2026-04-16 11:59:55   浏览量 :5
发布日期:2026-04-16 11:59:55  
5

2026西湖龙井茶官网DTC发售:茶农直供,政府溯源防伪到农户家 

请将以下 HTML 内容翻译成简体中文。要求: 1. 保留所有 HTML 标签不变(如

,

, ,
    ,
  • 等) 2. 保留所有图片的 src 和 alt 属性 3. 只翻译标签之间的文本内容 4. 所有英文必须翻译成中文,不保留英文原文 5. 输出完整的 HTML 代码 HTML 内容:

    500+ snapshot tests, all green on every developer's Mac, all red on GitHub Actions. Sound familiar?

    The common advice is "record your reference images on CI" or "lower your precision threshold." We tried both. Neither felt right.

    Recording on CI means you can't verify snapshots locally anymore. Every UI change becomes a multi-step ritual: push a commit, wait for CI to fail, download the new reference PNGs from the artifacts, commit them, push again, wait for CI to pass. If you touch 10 views, that's 10 PNGs you need to pull down and commit blind — you're trusting CI's rendering as ground truth without ever seeing the images on your own screen. And if two people change UI on separate branches, you get merge conflicts in binary PNG files.

    Lowering precision thresholds is worse. Drop to 85% and you're not really testing the UI anymore — real regressions hide in the noise.

    It took us three wrong hypotheses and a lot of diff images to find the real cause. Sharing in case it saves someone else the same journey.

    Why we moved from iOS Simulator to macOS

    TL;DR: Tests went from 170s to 7s locally (25x), CI from ~30 min to ~17 min.

    Before the snapshot story, some context on how we got here — because the move to macOS is what made this problem (and the fix) possible.

    Our test suite ran on the iOS Simulator. Every xcodebuild test invocation booted a simulator, waited for it to become ready, deployed the test bundle, and ran. 170 seconds for a full run. Locally that's annoying; on CI it's brutal — you're paying for a macOS runner to sit idle while a virtual iPhone boots.

    We started asking: how many of these tests actually need a simulator? We audited the suite and the answer was almost none. Our app logic — state management, data parsing, network handling, navigation — is pure Swift. It doesn't call UIKit. And SwiftUI views? They render just fine on macOS through NSHostingView. Apple's own framework handles the translation.

    So we flipped the destination from platform=iOS Simulator to platform=macOS and ran the suite. Most tests passed immediately. A handful needed #if os(iOS) guards — things like UIImage processing or CLAuthorizationStatus that genuinely require iOS APIs. We kept those on the simulator and moved everything else to macOS.

    The result: 7 seconds. Same tests, same assertions, 25x faster. The CI improvement was even more dramatic — we switched to a build-once pattern (build the test target, upload the build artifact, then fan out parallel test jobs using xcodebuild test-without-building). Total CI time dropped from ~30 minutes to ~17 minutes.

    Summary

    The logic tests worked perfectly on macOS. The snapshot tests did not.

    What we tried (and why it didn't work)

    Hypothesis 1: "It's the resolution"

    Retina Macs render at 2x. CI VMs (GitHub Actions macOS runners) render at 1x. We built a custom rendering strategy that pins the bitmap to a fixed size — 390x844 at 1x scale. This fixed the dimension mismatch, but tests still failed.

    Hypothesis 2: "It's font rendering"

    免责声明:本文内容来自互联网,该文观点不代表本站观点。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请到页面底部单击反馈,一经查实,本站将立刻删除。

关于我们
热门推荐
合作伙伴
免责声明:本站部分资讯来源于网络,如有侵权请及时联系客服,我们将尽快处理
支持 反馈 订阅 数据
回到顶部