Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 1 | Testing is an essential component of software development in Chromium, |
| 2 | it ensures Chrome is behaving as we expect, and is critical to find bugs and |
| 3 | regressions at early stage. |
| 4 | |
| 5 | This document covers the high level overview of testing in Chromium, |
| 6 | including what type of tests we have, what's the purpose for each test type, |
| 7 | what tests are needed for new features etc. |
| 8 | |
| 9 | ## Test Types |
| 10 | |
| 11 | There are several different types of tests in Chromium to serve different purposes, |
| 12 | some types of test are running on multiple platforms, others are specific |
| 13 | for one platform. |
| 14 | |
| 15 | * **[gtest]** is Google's C++ test framework, |
| 16 | which helps you write better C++ tests in Chromium. |
| 17 | gtest is test framework for unit tests in Chromium and browser tests are built on top of it. |
Weizhong Xia | aec7725 | 2025-04-24 18:39:44 | [diff] [blame] | 18 | * **[Browser Tests]** is built on top of gtest, and it is used to write integration tests |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 19 | and e2e tests in Chromium. |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 20 | * **[Web Tests] (formerly known as "Layout Tests" or "LayoutTests")** |
| 21 | is used by Blink to test many components, including but not |
| 22 | limited to layout and rendering. In general, web tests involve loading pages |
| 23 | in a test renderer (`content_shell`) and comparing the rendered output or |
| 24 | JavaScript output against an expected output file. |
| 25 | Web Tests are required to launch new W3C API support in Chromium. |
Andrew Grieve | b5b3bb3 | 2023-10-04 13:55:51 | [diff] [blame] | 26 | * **[Robolectric]** is build on top of JUnit 4. It emulates Android APIs so |
| 27 | that tests can be run on the host machine instead of on devices / emulators. |
| 28 | * **[Instrumentation Tests]** are JUnit tests that run on devices / emulators. |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 29 | * **[EarlGrey]** is the integration testing framework used by Chromium for iOS. |
| 30 | * **[Telemetry]** is the performance testing framework used by Chromium. |
| 31 | It allows you to perform arbitrary actions on a set of web pages and |
| 32 | report metrics about it. |
| 33 | * **[Fuzzer Tests]** is used to uncover potential security & stability problems in Chromium. |
Ben Pastene | b30c66e9 | 2019-08-30 23:25:10 | [diff] [blame] | 34 | * **[Tast]** is a test framework for system integration tests on Chrome OS. |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 35 | |
| 36 | |
| 37 | The following table shows which types of test works on which platforms. |
| 38 | |
| 39 | | | Linux | Windows | Mac | Android | iOS | CrOS | |
| 40 | |:----------------------------|:--------|:--------|:--------|:--------|:--------|:--------| |
| 41 | | gtest(C++) | √ | √ | √ | √ | √ | √ | |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 42 | | Browser Tests(C++) | √ | √ | √ | √ | | | |
| 43 | | Web Tests(HTML, JS) | √ | √ | √ | | | | |
Ben Pastene | b30c66e9 | 2019-08-30 23:25:10 | [diff] [blame] | 44 | | Telemetry(Python) | √ | √ | √ | √ | | √ | |
Andrew Grieve | b5b3bb3 | 2023-10-04 13:55:51 | [diff] [blame] | 45 | | Robolectric(Java) | | | | √ | | | |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 46 | | Instrumentation Tests(Java) | | | | √ | | | |
| 47 | | EarlGrey | | | | | √ | | |
| 48 | | Fuzzer Tests(C++) | √ | √ | √ | √ | | √ | |
Ben Pastene | b30c66e9 | 2019-08-30 23:25:10 | [diff] [blame] | 49 | | Tast(Golang) | | | | | | √ | |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 50 | |
| 51 | *** note |
| 52 | **Browser Tests Note** |
| 53 | |
| 54 | Only subset of browser tests are enabled on Android: |
| 55 | * components_browsertests |
| 56 | * content_browsertests |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 57 | |
| 58 | Other browser tests are not supported on Android yet. [crbug/611756] |
| 59 | tracks the effort to enable them on Android. |
| 60 | *** |
| 61 | |
| 62 | *** note |
| 63 | **Web Tests Note** |
| 64 | |
| 65 | Web Tests were enabled on Android K before, but it is disabled on Android platform now, |
| 66 | see [this thread](https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/338WKwWPbPI/discussion) for more context. |
| 67 | *** |
| 68 | |
John Abd-El-Malek | 33524c2 | 2024-01-29 19:41:23 | [diff] [blame] | 69 | *** note |
| 70 | **Tast Tests Note** |
| 71 | |
| 72 | Tast tests are written, maintained and gardened by ChromeOS engineers. |
| 73 | |
| 74 | ChromeOS tests that Chrome engineers support should be (re)written in the following priority order: |
| 75 | * unit tests |
| 76 | * linux_chromeos browser_tests |
| 77 | * linux_chromeos interactive_ui_tests |
| 78 | * [Crosier tests](http://go/crosier) |
| 79 | |
| 80 | When a Tast test fails: |
| 81 | * If the change is written by a ChromeOS engineer, the ChromeOS [gardener](http://go/cros-gardening) can revert it. |
| 82 | * Otherwise the ChromeOS gardener can revert the Chrome-authored change accompanied by a test from the supported frameworks above or manual repro steps that a Chrome engineer can run under [linux_chromeos](../chromeos_build_instructions.md#Chromium-OS-on-Linux-linux_chromeos) (preferable) or using Simple Chrome VM [instructions](https://chromium.googlesource.com/chromiumos/docs/+/HEAD/simple_chrome_workflow.md). |
| 83 | * If the above is not possible, the ChromeOS gardener or ChromeOS feature owner should inform the author about the failure, and the author uses their best judgment on whether to revert, fix on trunk, or let ChromeOS engineers update the test (e.g. if test needs an update or if the test is just testing internal implementation details of Chrome but doesn't break user functionality). |
| 84 | |
| 85 | |
| 86 | *** |
| 87 | |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 88 | ## General Principles |
| 89 | |
| 90 | * All the tests in Chromium running on CQ and main waterfall should be hermetic and stable. |
| 91 | * Add unit tests along with the code in same changelist instead of adding tests in future, |
| 92 | it is most likely no one will add tests later. |
| 93 | * Write enough unit tests to have good [code coverage](./code_coverage.md), |
| 94 | since they are fast and stable. |
| 95 | * Don't enable tests with external dependencies on CQ and main waterfall, |
| 96 | e.g. tests against live sites. |
| 97 | It is fine to check in those tests, but only run them on your own bots. |
Dirk Pranke | e034e1ef5 | 2020-07-03 00:48:08 | [diff] [blame] | 98 | * Eventually, all tests should implement the |
| 99 | [Test Executable API](./test_executable_api.md) command line interface. |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 100 | |
| 101 | ## What tests are needed for new features |
| 102 | |
| 103 | * **Unit Tests** are needed no matter where the code is for your feature. |
| 104 | It is the best practice to add the unit tests |
| 105 | when you add new code or update existing code in the same changelist, |
Dirk Pranke | e034e1ef5 | 2020-07-03 00:48:08 | [diff] [blame] | 106 | check out [Code Coverage in Gerrit](./code_coverage_in_gerrit.md) |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 107 | for the instruction about how to see the code coverage in Gerrit. |
| 108 | * **Browser Tests** are recommended for integration tests and e2e tests. |
| 109 | It will be great if you add browser tests to cover the major user |
| 110 | cases for your feature, even with some mocking. |
| 111 | * **[Web Tests]** are required if you plan to launch new W3C APIs in Chrome. |
| 112 | * **[Instrumentation Tests]** are recommended for features on Android, you only |
| 113 | need to write instrumentation features |
| 114 | if your feature is supported on Android for integration tests or e2e tests. |
| 115 | * **EarlGrey Tests** are recommended for iOS only. |
| 116 | * **[Telemetry] benchmarking or stories** are needed if existing telemetry |
| 117 | benchmarks or stories can't cover the performance for your feature, |
| 118 | you need to either add new story, but reuse existing metrics or |
| 119 | add new benchmarks for your feature. Talk to benchmarking team first |
| 120 | before start to add Telemetry benchmarks or stories. |
| 121 | * **[Fuzzer Tests]** are recommended if your feature adds user facing APIs |
| 122 | in Chromium, it is recommended to write fuzzer tests to detect the security issue. |
| 123 | |
| 124 | Right now, code coverage is the only way we have to measure test coverage. |
| 125 | The following is the recommended thresholds for different code coverage levels: |
| 126 | * >level 1(improving): >0% |
| 127 | * >level 2(acceptable): 60% |
| 128 | * >level 3(commendable): 75% |
| 129 | * >level 4(exemplary): 90% |
| 130 | |
Bastian Kersting | 1aef80b | 2022-05-13 10:34:35 | [diff] [blame] | 131 | Go to [code coverage dashboard](https://analysis.chromium.org/coverage/p/chromium) to check the code coverage for your project. |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 132 | |
| 133 | |
| 134 | ## How to write new tests |
| 135 | * [Simple gtests] |
| 136 | * [Writing JUnit tests] |
| 137 | * [Writing Browser Tests] |
| 138 | * [Writing Instrumentation Tests] |
| 139 | * [Writing EarlGrey Tests] |
| 140 | * [Writing Telemetry Benchmarks/Stories] |
| 141 | * [Writing Web Tests](./writing_web_tests.md) |
| 142 | * [Write Fuzz Target] |
| 143 | |
| 144 | >TODO: add the link to the instruction about how to enable new tests in CQ and main waterfall |
| 145 | |
| 146 | ## How to run tests |
| 147 | |
| 148 | ### Run tests locally |
Delan Azabani | b4a421a | 2021-05-26 08:46:01 | [diff] [blame] | 149 | * [Run gtest locally](#Run-gtest-locally) |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 150 | * [Run browser tests locally] |
| 151 | * [Run tests on Android](./android_test_instructions.md#Running-Tests) |
| 152 | It includes the instructions to run gTests, JUnit tests and Instrumentation tests on Android. |
| 153 | * [Run EarlGrey tests locally](../ios/testing.md#running-tests-from-xcode) |
Delan Azabani | b4a421a | 2021-05-26 08:46:01 | [diff] [blame] | 154 | * [Run Web Tests locally](./web_tests.md#running-web-tests) |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 155 | * [Telemetry: Run benchmarks locally] |
| 156 | * [Run fuzz target locally] |
| 157 | |
Delan Azabani | b4a421a | 2021-05-26 08:46:01 | [diff] [blame] | 158 | #### Run gtest locally |
| 159 | |
| 160 | Before you can run a gtest, you need to build the appropriate launcher target |
| 161 | that contains your test, such as `blink_unittests`: |
| 162 | |
| 163 | ```bash |
| 164 | autoninja -C out/Default blink_unittests |
| 165 | ``` |
| 166 | |
| 167 | To run specific tests, rather than all tests in a launcher, pass |
| 168 | `--gtest_filter=` with a pattern. The simplest pattern is the full name of a |
| 169 | test (SuiteOrFixtureName.TestName), but you can use wildcards: |
| 170 | |
| 171 | ```bash |
| 172 | out/Default/blink_unittests --gtest_filter='Foo*' |
| 173 | ``` |
| 174 | |
| 175 | Use `--help` for more ways to select and run tests. |
| 176 | |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 177 | ### Run tests remotely(on Swarming) |
| 178 | >TODO: add the link to the instruction about how to run tests on Swarming. |
| 179 | |
| 180 | ## How to debug tests |
| 181 | * [Android Debugging Instructions] |
Ben Pastene | b30c66e9 | 2019-08-30 23:25:10 | [diff] [blame] | 182 | * [Chrome OS Debugging Tips] |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 183 | * [Debugging Web Tests] |
| 184 | |
| 185 | ## How to deal with flaky tests |
| 186 | |
Matthew Warton | c9908ca | 2022-11-01 17:20:54 | [diff] [blame] | 187 | Go to [LUCI Analysis] to find reports about flaky tests in your projects. |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 188 | |
Stephen McGruer | 5f4af313 | 2020-11-05 21:05:52 | [diff] [blame] | 189 | * [Addressing Flaky GTests](./gtest_flake_tips.md) |
| 190 | * [Addressing Flaky Web Tests](./web_tests_addressing_flake.md) |
| 191 | * [Addressing Flaky WPTs](./web_platform_tests_addressing_flake.md) |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 192 | |
Stephen McGruer | 5f4af313 | 2020-11-05 21:05:52 | [diff] [blame] | 193 | If you cannot fix a flaky test in a short timeframe, disable it first to reduce |
| 194 | development pain for other and then fix it later. "[How do I disable a flaky |
| 195 | test]" has instructions on how to disable a flaky test. |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 196 | |
Robert Kaplow | 8202763 | 2023-02-13 16:31:52 | [diff] [blame] | 197 | ## Other |
| 198 | |
| 199 | Tests are not configured to upload metrics, such as UMA, UKM or crash reports. |
| 200 | |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 201 | [gtest]: https://github.com/google/googletest |
Eric Willigers | c2e3d881 | 2022-01-17 01:59:37 | [diff] [blame] | 202 | [Simple gtests]: https://github.com/google/googletest/blob/main/docs/primer.md#simple-tests |
Weizhong Xia | aec7725 | 2025-04-24 18:39:44 | [diff] [blame] | 203 | [Browser Tests]: https://chromium.googlesource.com/website/+/HEAD/site/developers/testing/browser-tests/index.md |
Andrew Grieve | b5b3bb3 | 2023-10-04 13:55:51 | [diff] [blame] | 204 | [Robolectric]: android_robolectric_tests.md |
| 205 | [Instrumentation Tests]: https://chromium.googlesource.com/chromium/src/+/main/docs/testing/android_instrumentation_tests.md |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 206 | [EarlGrey]: https://github.com/google/EarlGrey |
| 207 | [Telemetry]: https://chromium.googlesource.com/catapult/+/HEAD/telemetry/README.md |
John Palmer | 046f987 | 2021-05-24 01:24:56 | [diff] [blame] | 208 | [Fuzzer Tests]: https://chromium.googlesource.com/chromium/src/+/main/testing/libfuzzer/README.md |
Ben Pastene | b30c66e9 | 2019-08-30 23:25:10 | [diff] [blame] | 209 | [Tast]: https://chromium.googlesource.com/chromiumos/platform/tast/+/HEAD/README.md |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 210 | [Web Tests]: ./web_tests.md |
| 211 | [crbug/611756]: https://bugs.chromium.org/p/chromium/issues/detail?id=611756 |
Matthew Warton | c9908ca | 2022-11-01 17:20:54 | [diff] [blame] | 212 | [LUCI Analysis]: https://luci-analysis.appspot.com/ |
John Palmer | 046f987 | 2021-05-24 01:24:56 | [diff] [blame] | 213 | [Write Fuzz Target]: https://chromium.googlesource.com/chromium/src/+/main/testing/libfuzzer/getting_started.md#write-fuzz-target |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 214 | [Telemetry: Run benchmarks locally]: https://chromium.googlesource.com/catapult/+/HEAD/telemetry/docs/run_benchmarks_locally.md |
John Palmer | 046f987 | 2021-05-24 01:24:56 | [diff] [blame] | 215 | [Run fuzz target locally]: https://chromium.googlesource.com/chromium/src/+/main/testing/libfuzzer/getting_started.md#build-and-run-fuzz-target-locally |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 216 | [Android Debugging Instructions]: https://chromium.googlesource.com/chromium/src/+/HEAD/docs/android_debugging_instructions.md |
Ben Pastene | b30c66e9 | 2019-08-30 23:25:10 | [diff] [blame] | 217 | [Chrome OS Debugging Tips]: ./chromeos_debugging_tips.md |
Lei Lei | 75b4ff7 | 2019-08-21 19:03:54 | [diff] [blame] | 218 | [Debugging Web Tests]: https://chromium.googlesource.com/chromium/src/+/HEAD/docs/testing/web_tests.md#Debugging-Web-Tests |
| 219 | [code coverage dashboard]: https://analysis.chromium.org/p/chromium/coverage |
| 220 | [How do I disable a flaky test]: https://www.chromium.org/developers/tree-sheriffs/sheriff-details-chromium#TOC-How-do-I-disable-a-flaky-test- |