<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>Visual Testing · 仁才德</title>
    <link>https://jared.lynskey.co.nz/zh-cn/tags/visual-testing/</link>
    <description>关于软件工程与研发团队领导力的短文——绩效评估、招聘与团队扩张、CI/CD、DevOps 监控、智能体 AI。写于首尔。</description>
    <generator>Hugo</generator>
    <language>zh-CN</language>
    <copyright>© 2026 仁才德</copyright>
    <lastBuildDate>Wed, 28 Oct 2026 00:00:00 +1300</lastBuildDate>
    <atom:link href="https://jared.lynskey.co.nz/zh-cn/tags/visual-testing/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>用 Playwright 做视觉回归测试，小团队尤其值得</title>
      <link>https://jared.lynskey.co.nz/zh-cn/posts/2024/2024-06-09-playwright-tests/</link>
      <guid isPermaLink="true">https://jared.lynskey.co.nz/zh-cn/posts/2024/2024-06-09-playwright-tests/</guid>
      <pubDate>Sun, 09 Jun 2024 00:30:03 +0000</pubDate>
      <dc:creator>Jared Lynskey</dc:creator>
      <category>Visual Testing</category>
      <category>Small Teams</category>
      <category>UI Testing</category>
      <category>Playwright</category>
      <description>用 Playwright 做视觉回归测试：为什么截图对比是我们小团队性价比最高的测试。</description>
      <content:encoded><![CDATA[<p>只改了一行 CSS，另一个页面上的按钮就悄悄塌了。没有测试失败，代码审查也没看出来，一周后才从用户那里听说。小团队应该都遇到过这种 bug。</p>
<p>教科书式的答案是多写单元测试。我带着一个小开发团队，说实话，给每个组件、每种布局状态都写单元测试是我们负担不起的奢侈。我们靠的是视觉测试：给页面截图，和已知正常的基线对比，不该动的像素动了就在构建里报出来。考虑到投入的精力，我还没见过更划算的办法。</p>

<h2 class="relative group">截图为什么这么值
    <div id="截图为什么这么值" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#%e6%88%aa%e5%9b%be%e4%b8%ba%e4%bb%80%e4%b9%88%e8%bf%99%e4%b9%88%e5%80%bc" aria-label="锚点">#</a>
    </span>
    
</h2>
<p>它的魅力在于你不用写的那些东西。视觉测试不给单个元素写断言，而是把整个页面当成一个整体来判断。布局偏移、颜色变化、响应式破损、元素重叠——恰恰是代码审查容易漏掉、单元测试又很难覆盖的问题，现在全都以 diff 的形式出现在 PR 里。</p>
<p>维护成本也几乎为零。基线设一次，之后每个 PR 自动跑，每次结果一致。有意的 UI 改动？更新基线，继续干活。无意的改动？diff 就在 PR 里摆着，不用再一页一页点过去找问题。</p>

<h2 class="relative group">它替代不了单元测试
    <div id="它替代不了单元测试" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#%e5%ae%83%e6%9b%bf%e4%bb%a3%e4%b8%8d%e4%ba%86%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95" aria-label="锚点">#</a>
    </span>
    
</h2>
<p>单元测试验证逻辑，截图验证外观，最终两个都要有。但如果团队小，必须选择先把时间投在哪里，同样的搭建时间，视觉测试换来的覆盖面比什么都大。我的原则是：坏了会真正出事的业务逻辑用单元测试，其余交给视觉测试。</p>
<p>整套系统就这些：基线、每个 PR 的 diff，偶尔点一下“是的，这是有意的改动”。就这点投入来说，它抓到的问题多得不像话。</p>

<h2 class="relative group">更多信息和实用链接
    <div id="更多信息和实用链接" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#%e6%9b%b4%e5%a4%9a%e4%bf%a1%e6%81%af%e5%92%8c%e5%ae%9e%e7%94%a8%e9%93%be%e6%8e%a5" aria-label="锚点">#</a>
    </span>
    
</h2>

<h3 class="relative group">Visual Testing with Playwright
    <div id="visual-testing-with-playwright" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#visual-testing-with-playwright" aria-label="锚点">#</a>
    </span>
    
</h3>
<p>Playwright 的快照测试文档：截取整页或特定元素，与基线对比，并在有意改动后管理基线更新。
<a href="https://playwright.dev/docs/test-snapshots"  target="_blank" rel="noreferrer">Visual Testing with Playwright</a></p>

<h3 class="relative group">Best Practices for Testing with Playwright
    <div id="best-practices-for-testing-with-playwright" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#best-practices-for-testing-with-playwright" aria-label="锚点">#</a>
    </span>
    
</h3>
<p>Playwright 关于数据库相关测试的建议：隔离测试数据、测试后清理、保持测试相互独立，让测试套件稳定可靠。
<a href="https://playwright.dev/docs/best-practices#testing-with-a-database"  target="_blank" rel="noreferrer">Best Practices for Testing with Playwright</a></p>
]]></content:encoded>
      <media:content url="https://jared.lynskey.co.nz/en/posts/2024/2024-06-09-playwright-tests/featured.jpg" medium="image" type="image/jpeg" />
    </item>
  </channel>
</rss>
