blob: 5dd9215d7164743aadeb622ebbb91471162dbf13 [file] [log] [blame] [view]
Lei Lei75b4ff72019-08-21 19:03:541Testing is an essential component of software development in Chromium,
2it ensures Chrome is behaving as we expect, and is critical to find bugs and
3regressions at early stage.
4
5This document covers the high level overview of testing in Chromium,
6including what type of tests we have, what's the purpose for each test type,
7what tests are needed for new features etc.
8
9## Test Types
10
11There are several different types of tests in Chromium to serve different purposes,
12some types of test are running on multiple platforms, others are specific
13for 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.
Lei Lei75b4ff72019-08-21 19:03:5418* **Browser Tests** is built on top of gtest, and it is used to write integration tests
19 and e2e tests in Chromium.
20 <!-- TODO(leilei) Add link to browser tests --->
21* **[Web Tests] (formerly known as "Layout Tests" or "LayoutTests")**
22 is used by Blink to test many components, including but not
23 limited to layout and rendering. In general, web tests involve loading pages
24 in a test renderer (`content_shell`) and comparing the rendered output or
25 JavaScript output against an expected output file.
26 Web Tests are required to launch new W3C API support in Chromium.
Andrew Grieveb5b3bb32023-10-04 13:55:5127* **[Robolectric]** is build on top of JUnit 4. It emulates Android APIs so
28 that tests can be run on the host machine instead of on devices / emulators.
29* **[Instrumentation Tests]** are JUnit tests that run on devices / emulators.
Lei Lei75b4ff72019-08-21 19:03:5430* **[EarlGrey]** is the integration testing framework used by Chromium for iOS.
31* **[Telemetry]** is the performance testing framework used by Chromium.
32 It allows you to perform arbitrary actions on a set of web pages and
33 report metrics about it.
34* **[Fuzzer Tests]** is used to uncover potential security & stability problems in Chromium.
Ben Pasteneb30c66e92019-08-30 23:25:1035* **[Tast]** is a test framework for system integration tests on Chrome OS.
Lei Lei75b4ff72019-08-21 19:03:5436
37
38The following table shows which types of test works on which platforms.
39
40| | Linux | Windows | Mac | Android | iOS | CrOS |
41|:----------------------------|:--------|:--------|:--------|:--------|:--------|:--------|
42| gtest(C++) | &#8730; | &#8730; | &#8730; | &#8730; | &#8730; | &#8730; |
Lei Lei75b4ff72019-08-21 19:03:5443| Browser Tests(C++) | &#8730; | &#8730; | &#8730; | &#8730; | | |
44| Web Tests(HTML, JS) | &#8730; | &#8730; | &#8730; | | | |
Ben Pasteneb30c66e92019-08-30 23:25:1045| Telemetry(Python) | &#8730; | &#8730; | &#8730; | &#8730; | | &#8730; |
Andrew Grieveb5b3bb32023-10-04 13:55:5146| Robolectric(Java) | | | | &#8730; | | |
Lei Lei75b4ff72019-08-21 19:03:5447| Instrumentation Tests(Java) | | | | &#8730; | | |
48| EarlGrey | | | | | &#8730; | |
49| Fuzzer Tests(C++) | &#8730; | &#8730; | &#8730; | &#8730; | | &#8730; |
Ben Pasteneb30c66e92019-08-30 23:25:1050| Tast(Golang) | | | | | | &#8730; |
Lei Lei75b4ff72019-08-21 19:03:5451
52*** note
53**Browser Tests Note**
54
55Only subset of browser tests are enabled on Android:
56* components_browsertests
57* content_browsertests
Lei Lei75b4ff72019-08-21 19:03:5458
59Other browser tests are not supported on Android yet. [crbug/611756]
60tracks the effort to enable them on Android.
61***
62
63*** note
64**Web Tests Note**
65
66Web Tests were enabled on Android K before, but it is disabled on Android platform now,
67see [this thread](https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/338WKwWPbPI/discussion) for more context.
68***
69
John Abd-El-Malek33524c22024-01-29 19:41:2370*** note
71**Tast Tests Note**
72
73Tast tests are written, maintained and gardened by ChromeOS engineers.
74
75ChromeOS tests that Chrome engineers support should be (re)written in the following priority order:
76* unit tests
77* linux_chromeos browser_tests
78* linux_chromeos interactive_ui_tests
79* [Crosier tests](http://go/crosier)
80
81When a Tast test fails:
82* If the change is written by a ChromeOS engineer, the ChromeOS [gardener](http://go/cros-gardening) can revert it.
83* 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).
84* 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).
85
86
87***
88
Lei Lei75b4ff72019-08-21 19:03:5489## General Principles
90
91* All the tests in Chromium running on CQ and main waterfall should be hermetic and stable.
92* Add unit tests along with the code in same changelist instead of adding tests in future,
93 it is most likely no one will add tests later.
94* Write enough unit tests to have good [code coverage](./code_coverage.md),
95 since they are fast and stable.
96* Don't enable tests with external dependencies on CQ and main waterfall,
97 e.g. tests against live sites.
98 It is fine to check in those tests, but only run them on your own bots.
Dirk Prankee034e1ef52020-07-03 00:48:0899* Eventually, all tests should implement the
100 [Test Executable API](./test_executable_api.md) command line interface.
Lei Lei75b4ff72019-08-21 19:03:54101
102## What tests are needed for new features
103
104* **Unit Tests** are needed no matter where the code is for your feature.
105 It is the best practice to add the unit tests
106 when you add new code or update existing code in the same changelist,
Dirk Prankee034e1ef52020-07-03 00:48:08107 check out [Code Coverage in Gerrit](./code_coverage_in_gerrit.md)
Lei Lei75b4ff72019-08-21 19:03:54108 for the instruction about how to see the code coverage in Gerrit.
109* **Browser Tests** are recommended for integration tests and e2e tests.
110 It will be great if you add browser tests to cover the major user
111 cases for your feature, even with some mocking.
112* **[Web Tests]** are required if you plan to launch new W3C APIs in Chrome.
113* **[Instrumentation Tests]** are recommended for features on Android, you only
114 need to write instrumentation features
115 if your feature is supported on Android for integration tests or e2e tests.
116* **EarlGrey Tests** are recommended for iOS only.
117* **[Telemetry] benchmarking or stories** are needed if existing telemetry
118 benchmarks or stories can't cover the performance for your feature,
119 you need to either add new story, but reuse existing metrics or
120 add new benchmarks for your feature. Talk to benchmarking team first
121 before start to add Telemetry benchmarks or stories.
122* **[Fuzzer Tests]** are recommended if your feature adds user facing APIs
123 in Chromium, it is recommended to write fuzzer tests to detect the security issue.
124
125Right now, code coverage is the only way we have to measure test coverage.
126The following is the recommended thresholds for different code coverage levels:
127* >level 1(improving): >0%
128* >level 2(acceptable): 60%
129* >level 3(commendable): 75%
130* >level 4(exemplary): 90%
131
Bastian Kersting1aef80b2022-05-13 10:34:35132Go to [code coverage dashboard](https://analysis.chromium.org/coverage/p/chromium) to check the code coverage for your project.
Lei Lei75b4ff72019-08-21 19:03:54133
134
135## How to write new tests
136* [Simple gtests]
137* [Writing JUnit tests]
138* [Writing Browser Tests]
139* [Writing Instrumentation Tests]
140* [Writing EarlGrey Tests]
141* [Writing Telemetry Benchmarks/Stories]
142* [Writing Web Tests](./writing_web_tests.md)
143* [Write Fuzz Target]
144
145>TODO: add the link to the instruction about how to enable new tests in CQ and main waterfall
146
147## How to run tests
148
149### Run tests locally
Delan Azabanib4a421a2021-05-26 08:46:01150* [Run gtest locally](#Run-gtest-locally)
Lei Lei75b4ff72019-08-21 19:03:54151* [Run browser tests locally]
152* [Run tests on Android](./android_test_instructions.md#Running-Tests)
153 It includes the instructions to run gTests, JUnit tests and Instrumentation tests on Android.
154* [Run EarlGrey tests locally](../ios/testing.md#running-tests-from-xcode)
Delan Azabanib4a421a2021-05-26 08:46:01155* [Run Web Tests locally](./web_tests.md#running-web-tests)
Lei Lei75b4ff72019-08-21 19:03:54156* [Telemetry: Run benchmarks locally]
157* [Run fuzz target locally]
158
Delan Azabanib4a421a2021-05-26 08:46:01159#### Run gtest locally
160
161Before you can run a gtest, you need to build the appropriate launcher target
162that contains your test, such as `blink_unittests`:
163
164```bash
165autoninja -C out/Default blink_unittests
166```
167
168To run specific tests, rather than all tests in a launcher, pass
169`--gtest_filter=` with a pattern. The simplest pattern is the full name of a
170test (SuiteOrFixtureName.TestName), but you can use wildcards:
171
172```bash
173out/Default/blink_unittests --gtest_filter='Foo*'
174```
175
176Use `--help` for more ways to select and run tests.
177
Lei Lei75b4ff72019-08-21 19:03:54178### Run tests remotely(on Swarming)
179>TODO: add the link to the instruction about how to run tests on Swarming.
180
181## How to debug tests
182* [Android Debugging Instructions]
Ben Pasteneb30c66e92019-08-30 23:25:10183* [Chrome OS Debugging Tips]
Lei Lei75b4ff72019-08-21 19:03:54184* [Debugging Web Tests]
185
186## How to deal with flaky tests
187
Matthew Wartonc9908ca2022-11-01 17:20:54188Go to [LUCI Analysis] to find reports about flaky tests in your projects.
Lei Lei75b4ff72019-08-21 19:03:54189
Stephen McGruer5f4af3132020-11-05 21:05:52190* [Addressing Flaky GTests](./gtest_flake_tips.md)
191* [Addressing Flaky Web Tests](./web_tests_addressing_flake.md)
192* [Addressing Flaky WPTs](./web_platform_tests_addressing_flake.md)
Lei Lei75b4ff72019-08-21 19:03:54193
Stephen McGruer5f4af3132020-11-05 21:05:52194If you cannot fix a flaky test in a short timeframe, disable it first to reduce
195development pain for other and then fix it later. "[How do I disable a flaky
196test]" has instructions on how to disable a flaky test.
Lei Lei75b4ff72019-08-21 19:03:54197
Robert Kaplow82027632023-02-13 16:31:52198## Other
199
200Tests are not configured to upload metrics, such as UMA, UKM or crash reports.
201
Lei Lei75b4ff72019-08-21 19:03:54202[gtest]: https://github.com/google/googletest
Eric Willigersc2e3d8812022-01-17 01:59:37203[Simple gtests]: https://github.com/google/googletest/blob/main/docs/primer.md#simple-tests
Andrew Grieveb5b3bb32023-10-04 13:55:51204[Robolectric]: android_robolectric_tests.md
205[Instrumentation Tests]: https://chromium.googlesource.com/chromium/src/+/main/docs/testing/android_instrumentation_tests.md
Lei Lei75b4ff72019-08-21 19:03:54206[EarlGrey]: https://github.com/google/EarlGrey
207[Telemetry]: https://chromium.googlesource.com/catapult/+/HEAD/telemetry/README.md
John Palmer046f9872021-05-24 01:24:56208[Fuzzer Tests]: https://chromium.googlesource.com/chromium/src/+/main/testing/libfuzzer/README.md
Ben Pasteneb30c66e92019-08-30 23:25:10209[Tast]: https://chromium.googlesource.com/chromiumos/platform/tast/+/HEAD/README.md
Lei Lei75b4ff72019-08-21 19:03:54210[Web Tests]: ./web_tests.md
211[crbug/611756]: https://bugs.chromium.org/p/chromium/issues/detail?id=611756
Matthew Wartonc9908ca2022-11-01 17:20:54212[LUCI Analysis]: https://luci-analysis.appspot.com/
John Palmer046f9872021-05-24 01:24:56213[Write Fuzz Target]: https://chromium.googlesource.com/chromium/src/+/main/testing/libfuzzer/getting_started.md#write-fuzz-target
Lei Lei75b4ff72019-08-21 19:03:54214[Telemetry: Run benchmarks locally]: https://chromium.googlesource.com/catapult/+/HEAD/telemetry/docs/run_benchmarks_locally.md
John Palmer046f9872021-05-24 01:24:56215[Run fuzz target locally]: https://chromium.googlesource.com/chromium/src/+/main/testing/libfuzzer/getting_started.md#build-and-run-fuzz-target-locally
Lei Lei75b4ff72019-08-21 19:03:54216[Android Debugging Instructions]: https://chromium.googlesource.com/chromium/src/+/HEAD/docs/android_debugging_instructions.md
Ben Pasteneb30c66e92019-08-30 23:25:10217[Chrome OS Debugging Tips]: ./chromeos_debugging_tips.md
Lei Lei75b4ff72019-08-21 19:03:54218[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-