[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Screenshot testing is a very effective way to verify your app's UI. Screenshot\ntests can exist in Component, Feature and Application tests.\n\nYou can use third-party tools to create both instrumented and local screenshot\ntests. If you use Compose, you can use the official [Compose Preview Screenshot\ntesting tool](/studio/preview/compose-screenshot-testing).\n| **Important:** Screenshot testing is the recommended way to verify visual attributes in Compose UIs.\n\nDefinition\n----------\n\nScreenshot tests take a screenshot of a UI and compare it with a\npreviously-approved image, called \"reference\" or \"golden\":\n**Figure 1.** A screenshot test compares two images\n\nIf the images are the same, the test passes. If there are differences between\nthem, the tool creates a report:\n**Figure 2.** A screenshot test report, showing the reference and new screenshot on either side, and the difference in the middle.\n\nWith the report, you have two possible responses:\n\n- Realize that there is an error in the new code and fix it.\n- Approve the new screenshot and replace the reference image with the new one.\n\nScreenshot tests have a different workflow than regular tests because a failed\ntest doesn't always mean that there is an error.\n| **Note:** the term *screenshot* testing is preferred over *snapshot* testing when referring to image comparison.\n\nAdvantages\n----------\n\nThe advantages or screenshot tests are:\n\n- A screenshot test does multiple assertions per test. For example, a single test can check colors, margins, sizes, and fonts.\n- A screenshot test is much easier to write, understand, and maintain than an equivalent behavior test.\n- They are especially useful when verifying and catching regressions on different screen sizes.\n\nDisadvantages\n-------------\n\nHowever, screenshot tests can also have disadvantages:\n\n- Dealing with the reference images can be cumbersome, as a big project might end up with thousands of PNGs.\n- Different platforms (Linux, Max, and Windows) produce slightly different screenshots.\n- They are slower than equivalent behavior tests.\n- Having a large number of screenshot tests can cause problems, for example when a single change affects thousands of screenshots.\n\nThe following sections provide recommendations on addressing these issues.\n\nKeep screenshot tests to a minimum\n----------------------------------\n\nYou should minimize the number of screenshot tests while maximizing the feedback\nand coverage for regressions.\n\nCombinations of different UI states can grow the number of tests very quickly.\nThe following are some of the ways you can verify a part of your app's UI:\n\n- On different themes\n- Using different font sizes\n- Inside different screen sizes or bounds\n\nIf you do this for every component, layout and screen of your app, you end up\nwith thousands of screenshot files, most of which don't give you any extra\nfeedback.\n\nFor example, if you want to test a custom button with light and dark themes, and\nwith 3 font sizes, you don't need to create combinations of all of them.\nInstead, you can choose just one of the themes. This is because the way the\nbutton reacts to long words has no effect on the theme.\n**Figure 3.** You can omit some combinations of UI properties. **Key Point:** Avoid combinations of all the different scenarios in screenshot tests. Instead, choose screenshots that give you unique feedback.\n\nStore reference images\n----------------------\n\nReference (or golden) images are usually PNG files that can be checked into your\nsource control. However Git and most source control managers are optimized for\ntext files, not large binary files.\n\nYou have 3 options to manage these files:\n\n- Keep using git, but trying to minimize storage use.\n- Use [Git LFS](https://git-lfs.com/).\n- Use a cloud service to manage the screenshots.\n\n| **Key Point:** Start by checking your reference images into your repository, but limit their overall count. Revisit if you end up with too many over time.\n\nPlatform differences\n--------------------\n\nScreenshot tests rely on low-level platform APIs to draw specific features like\ntext or shadows, and platforms can implement those in different ways. If you\ndevelop on a Mac and save new screenshots taken locally, you might see broken\ntests on a Linux CI machine.\n\nThere are 2 ways to work around the issue:\n\n- Tolerate small changes\n- Take screenshots on a server\n\n| **Warning:** Even if your whole team uses the same platform to generate the screenshots, changes in libraries, operating systems or hardware architecture can create small changes.\n\n### Tolerate small changes\n\nYou can configure most screenshot testing libraries to allow for small\ndifferences when comparing two screenshots.\n\nThere are two approaches to this:\n\n- Configure a tolerance based on a percentage of modified pixels or a percentage of total difference in pixel values.\n- Use a smart differ---the algorithm that compares the screenshots---to verify structural and semantic similarity instead of pixels.\n\nThe disadvantage of this approach is that it could create false positives, and\nnot catch errors that are either below the threshold, or erroneously considered\nsimilar enough.\n\n### Take screenshots on a server\n\nTo use a pixel-perfect screenshot comparator, you must make sure that your tests\ntake screenshots in the same conditions. To do so, you can use your Continuous\nIntegration (CI) system or employ a cloud service.\n\nFor example, you can create a step in your CI workflow that does the following:\n\n1. Runs the screenshot tests (only necessary when not using pixel-perfect matching).\n2. Records new screenshots if the previous step failed.\n3. Commits the new files into the branch.\n\n**Figure 4.** Diagram showing how to take screenshots on CI\n\nUsing this approach, the screenshot tests never fail on CI, but it modifies the\nchange for you. This way you and the change reviewers can accept the new\nscreenshots by merging the change.\n\nScreenshot testing tools\n------------------------\n\nConsider the key following key differences between the available tools and\nlibraries for screenshot testing:\n\n- Environment: Local tests that run on the host, or instrumented tests that run on an emulator or device.\n- Rendering engine: Host-side screenshot solutions can use Layoutlib---Android Studio's rendering engine for previews---or Robolectric Native Graphics (RNG).\n - Layoutlib-based frameworks are focused on rendering static components, using different states to show different behavior. They're typically easier to use.\n - Frameworks that integrate with RNG can use all the features from Robolectric, allowing for tests with a bigger scope.\n\n| **Note:** The experimental [Compose Preview Screenshot Testing](/studio/preview/compose-screenshot-testing) tool is an integration with Compose Previews, which runs the tests on the host, using Layoutlib to render the screenshots."]]