Dirk Pranke | 78557c5 | 2020-09-26 19:04:09 | [diff] [blame] | 1 | # On disabling tests |
| 2 | |
| 3 | Sometimes you don't want to run a test that you've written (or that |
Dirk Pranke | 8b9103a | 2020-10-07 18:06:29 | [diff] [blame] | 4 | you've imported, like conformance tests). The test might not be possible to |
| 5 | run in a particular configuration, or be temporarily broken by another |
| 6 | change, or be flaky, or simply not work yet. In these cases (and perhaps others), |
| 7 | you should disable the test :). |
Dirk Pranke | 78557c5 | 2020-09-26 19:04:09 | [diff] [blame] | 8 | |
Dirk Pranke | 8b9103a | 2020-10-07 18:06:29 | [diff] [blame] | 9 | There are a number of different ways to do so: |
Dirk Pranke | 78557c5 | 2020-09-26 19:04:09 | [diff] [blame] | 10 | |
| 11 | * If the test is an entire binary or test suite, the first (and |
Dirk Pranke | 8b9103a | 2020-10-07 18:06:29 | [diff] [blame] | 12 | simplest) way is to simply not build (or build, but not run) |
| 13 | the test binary, of course. This makes sense for binaries that |
| 14 | are specific to particular build configurations (e.g., Android JUnit |
| 15 | tests don't need to be built on Windows). |
Dirk Pranke | 78557c5 | 2020-09-26 19:04:09 | [diff] [blame] | 16 | |
Dirk Pranke | 8b9103a | 2020-10-07 18:06:29 | [diff] [blame] | 17 | * A second way (for tests in C++) is to not compile a test in a |
| 18 | given configuration, e.g., `#ifndef WIN`. In this situation, the only |
Dirk Pranke | 78557c5 | 2020-09-26 19:04:09 | [diff] [blame] | 19 | way you would know the test existed and was disabled would be to |
Dirk Pranke | 8b9103a | 2020-10-07 18:06:29 | [diff] [blame] | 20 | parse the source code. We often do this today for tests that will |
| 21 | never be enabled in particular build configurations, but sometimes we do |
| 22 | this to temporarily skip tests as well. |
Dirk Pranke | 78557c5 | 2020-09-26 19:04:09 | [diff] [blame] | 23 | |
Dirk Pranke | 8b9103a | 2020-10-07 18:06:29 | [diff] [blame] | 24 | * A third way is to take advantage of features in your testing framework to |
| 25 | skip over tests. Examples include involve adding `DISABLED_` to the test |
| 26 | method name for GTest-based tests, `@unittest.skip` for Python-based tests, |
| 27 | or using the |
| 28 | [DisabledTest](../../base/test/android/javatests/src/org/chromium/base/test/DisabledTest.java) |
Boris Sazonov | c45f131 | 2022-09-06 16:01:50 | [diff] [blame] | 29 | annotation for JUnit-based Java tests (this works in both instrumentation |
| 30 | and Robolectric tests). In these cases, you don't run the test by default, |
| 31 | but you can determine the list of disabled tests at runtime because the |
| 32 | tests are present in the executable, and you may still be able to force the |
| 33 | test to be run via a command-line flag. |
Dirk Pranke | 78557c5 | 2020-09-26 19:04:09 | [diff] [blame] | 34 | |
Dirk Pranke | 8b9103a | 2020-10-07 18:06:29 | [diff] [blame] | 35 | * Fourth, for test frameworks that support |
| 36 | [expectations files or filter files](https://bit.ly/chromium-test-list-format), |
| 37 | you can use them to decide what to run and what to skip. This moves |
| 38 | the mechanisms out of the source code and into separate files; there are |
| 39 | advantages and disadvantages to this. The main advantage is that it |
| 40 | can make it easier to write tooling to disable tests, and the main |
| 41 | disadvantage is that it moves the mechanism away from the code it affects, |
| 42 | potentially making it harder to understand what's going on. |
Dirk Pranke | 78557c5 | 2020-09-26 19:04:09 | [diff] [blame] | 43 | |
Dirk Pranke | 8b9103a | 2020-10-07 18:06:29 | [diff] [blame] | 44 | * Finally, the test harness can run the test, but the test itself |
| 45 | might detect at runtime that it should exit early for some reason |
| 46 | rather than actually executing the code paths you'd normally want to |
| 47 | test. For example, if you have a test for some code path that requires |
| 48 | a GPU, but there's no GPU on the machine, the test might check for a |
| 49 | GPU and exit early with "success". |
Dirk Pranke | 78557c5 | 2020-09-26 19:04:09 | [diff] [blame] | 50 | |
Dirk Pranke | 8b9103a | 2020-10-07 18:06:29 | [diff] [blame] | 51 | If you want to be able to determine a global picture of which tests |
| 52 | were disabled, you can either parse BUILD files, expectations and filter |
| 53 | files, and source code to try and figure that out, or require the tests be |
| 54 | present in test binaries (i.e., not compiled out) and then run the test |
| 55 | binaries in order to collect the lists of disabled tests and report them |
| 56 | to a central system. |
Dirk Pranke | 78557c5 | 2020-09-26 19:04:09 | [diff] [blame] | 57 | |
Dirk Pranke | 8b9103a | 2020-10-07 18:06:29 | [diff] [blame] | 58 | Parsing code can be straightforward for some types of tests, but |
| 59 | difficult-to-impractical to do correctly for others. |