| Roland Levillain | 9d6864f | 2020-11-11 13:29:28 +0000 | [diff] [blame] | 1 | # Running ART Tests with Atest / Trade Federation |
| 2 | |
| 3 | ART Testing has early support for execution in the [Trade |
| 4 | Federation](https://source.android.com/devices/tech/test_infra/tradefed) |
| 5 | ("TradeFed") test harness, in particular via the |
| 6 | [Atest](https://source.android.com/compatibility/tests/development/atest) |
| 7 | command line tool. |
| 8 | |
| 9 | Atest conveniently takes care of building tests and their dependencies (using |
| 10 | Soong, the Android build system) and executing them using Trade Federation. |
| 11 | |
| 12 | See also [README.md](README.md) for a general introduction to ART run-tests and |
| 13 | gtests. |
| 14 | |
| 15 | ## ART run-tests |
| 16 | |
| 17 | ### Running ART run-tests on device |
| 18 | |
| 19 | ART run-tests are defined in sub-directories of `test/` starting with a number |
| 20 | (e.g. `test/001-HelloWorld`). Each ART run-test is identified in the build |
| 21 | system by a Soong module name following the `art-run-test-`*`<test-directory>`* |
| 22 | format (e.g. `art-run-test-001-HelloWorld`). |
| 23 | |
| 24 | You can run a specific ART run-test on device by passing its Soong module name |
| 25 | to Atest: |
| 26 | ```bash |
| 27 | atest art-run-test-001-HelloWorld |
| 28 | ``` |
| 29 | |
| 30 | To run all ART run-tests in a single command, the currently recommended way is |
| 31 | to use [test mapping](#test-mapping) (see below). |
| 32 | |
| 33 | ## ART gtests |
| 34 | |
| 35 | ### Running ART gtests on device |
| 36 | |
| 37 | Because of current build- and link-related limitations, ART gtests can only run |
| 38 | as part of the Testing ART APEX (`com.android.art.testing.apex`) on device, |
| 39 | i.e. they have to be part of the ART APEX package itself to be able to build and |
| 40 | run properly. This means that it is not possible to test the ART APEX presently |
| 41 | residing on a device (either the original one, located in the "system" |
| 42 | partition, or an updated package, present in the "data" partition). |
| 43 | |
| 44 | There are two ways to run ART gtests on device: |
| 45 | * by installing the Testing ART APEX (i.e. manually "updating" the ART APEX on |
| 46 | device); or |
| 47 | * by setting up a `chroot` environment on the device, and "activating" the |
| 48 | Testing ART APEX in that environment. |
| 49 | |
| 50 | ### Running ART gtests on device by installing the Testing ART APEX |
| 51 | |
| 52 | You can run ART gtests on device with the ART APEX installation strategy by |
| 53 | using the following `atest` command: |
| 54 | |
| 55 | ```bash |
| 56 | atest ArtGtestsTargetInstallApex |
| 57 | ``` |
| 58 | |
| 59 | This command: |
| 60 | 1. builds the Testing ART APEX from the Android source tree (including the ART |
| 61 | gtests); |
| 62 | 2. installs the Testing ART APEX using `adb install`; |
| 63 | 3. reboots the device; |
| 64 | 4. runs the tests; and |
| 65 | 5. uninstalls the module. |
| 66 | |
| 67 | You can run the tests of a single ART gtest C++ class using the |
| 68 | `ArtGtestsTargetInstallApex:`*`<art-gtest-c++-class>`* syntax, e.g.: |
| 69 | ```bash |
| 70 | atest ArtGtestsTargetInstallApex:JniInternalTest |
| 71 | ``` |
| 72 | |
| 73 | This syntax also supports the use of wildcards, e.g.: |
| 74 | ```bash |
| 75 | atest ArtGtestsTargetInstallApex:*Test* |
| 76 | ``` |
| 77 | |
| 78 | You can also use Trade Federation options to run a subset of ART gtests, e.g.: |
| 79 | ```bash |
| 80 | atest ArtGtestsTargetInstallApex -- \ |
| 81 | --module ArtGtestsTargetInstallApex --test '*JniInternalTest*' |
| 82 | ``` |
| 83 | |
| 84 | You can also pass option `--gtest_filter` to the gtest binary to achieve a |
| 85 | similar effect: |
| 86 | ```bash |
| 87 | atest ArtGtestsTargetInstallApex -- \ |
| 88 | --test-arg com.android.tradefed.testtype.GTest:native-test-flag:"--gtest_filter=*JniInternalTest*" |
| 89 | ``` |
| 90 | |
| 91 | ### Running ART gtests on device using a `chroot` environment |
| 92 | |
| 93 | You can run ART gtests on device with the chroot-based strategy by using the |
| 94 | following `atest` command: |
| 95 | |
| 96 | ```bash |
| 97 | atest ArtGtestsTargetChroot |
| 98 | ``` |
| 99 | |
| 100 | This command: |
| 101 | 1. builds the Testing ART APEX from the Android source tree (including the ART |
| 102 | gtests) and all the necessary dependencies for the `chroot` environment; |
| 103 | 2. sets up a `chroot` environment on the device; |
| 104 | 3. "activates" the Testing ART APEX (and other APEXes that it depends on) in the |
| 105 | `chroot` environment; |
| 106 | 4. runs the tests within the `chroot` environment; and |
| 107 | 5. cleans up the environment (deactivates the APEXes and removes the `chroot` |
| 108 | environment). |
| 109 | |
| 110 | ## Test Mapping |
| 111 | |
| 112 | ART Testing supports the execution of tests via [Test |
| 113 | Mapping](https://source.android.com/compatibility/tests/development/test-mapping). |
| 114 | The tests declared in ART's [TEST_MAPPING](../TEST_MAPPING) file are executed |
| 115 | during pre-submit testing (when an ART changelist in Gerrit is verified by |
| 116 | Treehugger) and/or post-submit testing (when a given change is merged in the |
| 117 | Android code base), depending on the "test group" where a test is declared. |
| 118 | |
| 119 | ### Running tests via Test Mapping with Atest |
| 120 | |
| 121 | It is possible to run tests via test mapping locally using Atest. |
| 122 | |
| 123 | To run all the tests declared in ART's `TEST_MAPPING` file, use the following |
| 124 | command from the Android source tree top-level directory: |
| 125 | ```bash |
| 126 | atest --test-mapping art:all |
| 127 | ``` |
| 128 | In the previous command, `art` is the (relative) path to the directory |
| 129 | containing the `TEST_MAPPING` file listing the tests to run, while `all` means |
| 130 | that tests declared in all [test |
| 131 | groups](https://source.android.com/compatibility/tests/development/test-mapping#defining_test_groups) |
| 132 | shall be run. |
| 133 | |
| 134 | To only run tests executed during pre-submit testing, use: |
| 135 | ```bash |
| 136 | atest --test-mapping art:presubmit |
| 137 | ``` |
| 138 | |
| 139 | To only run tests executed during post-submit testing, use: |
| 140 | ```bash |
| 141 | atest --test-mapping art:postsubmit |
| 142 | ``` |