1# ART Testing 2 3There are two suites of tests in the Android Runtime (ART): 4* _ART run-tests_: Tests of the ART runtime using Dex bytecode (mostly written 5 in Java). 6* _ART gtests_: C++ tests exercising various aspects of ART. 7 8## ART run-tests 9 10ART run-tests are tests exercising the runtime using Dex bytecode. They are 11written in Java and/or [Smali](https://github.com/JesusFreke/smali) 12(compiled/assembled as Dex bytecode) and sometimes native code (written as C/C++ 13testing libraries). Some tests also make use of the 14[Jasmin](http://jasmin.sourceforge.net/) assembler or the 15[ASM](https://asm.ow2.io/) bytecode manipulation tool. Run-tests are 16executed on the ART runtime (`dalvikvm`), possibly preceded by a 17pre-optimization of the Dex code (using `dex2oat`). 18 19The run-tests are identified by directories in this `test` directory, named with 20a numeric prefix and containing an `info.txt` file. For most run tests, the 21sources are in the `src` subdirectory. Sources found in the `src2` directory are 22compiled separately but to the same output directory; this can be used to 23exercise "API mismatch" situations by replacing class files created in the first 24pass. The `src-ex` directory is built separately, and is intended for exercising 25class loaders. Resources can be stored in the `res` directory, which is 26distributed together with the executable files. 27 28The run-tests logic lives in the `test/run-test` Bash script. The execution of a 29run-test has three main parts: building the test, running the test, and checking 30the test's output. By default, these three steps are implemented by three Bash 31scripts located in the `test/etc` directory (`default-build`, `default-run`, and 32`default-check`). These scripts rely on environment variables set by 33`test/run-test`. 34 35The default logic for all of these these steps (build, run, check) is overridden 36if the test's directory contains a Bash script named after the step 37(i.e. `build`, `run`, or `check`). Note that the default logic of the "run" step 38is actually implemented in the "JAR runner" (`test/etc/run-test-jar`), invoked 39by `test/etc/default-run`. 40 41After the execution of a run-test, the check step's default behavior 42(implemented in `test/etc/default-check`) is to respectively compare its 43standard output and standard error with the contents of the 44`expected-stdout.txt` and `expected-stderr.txt` files contained in the test's 45directory; any mismatch triggers a test failure. 46 47The `test/run-test` script handles the execution of a single run-test in a given 48configuration. The Python script `test/testrunner/testrunner.py` is a convenient 49script handling the construction and execution of multiple tests in one 50configuration or more. 51 52To see the invocation options supported by `run-test` and `testrunner.py`, run 53these commands from the Android source top-level directory: 54```sh 55art/test/run-test --help 56``` 57```sh 58art/test/testrunner/testrunner.py --help 59``` 60 61### Checker tests 62 63Some ART run-tests, known as "Checker tests", perform additional checks on ART's 64compiler. They are identified by their name, which match the 65`^[0-9]+-checker-.*` regular expression (e.g. `004-checker-UnsafeTest18`). 66 67Checker assertions are annotations in a run-test's (Java and Smali) source files 68verifying the behavior of the ART compiler when compiling the corresponding Dex 69code. They are checked by the `checker` tool (see [directory 70`art/tools/checker`](https://cs.android.com/android/platform/superproject/+/master:art/tools/checker/)) 71against a c1visualizer-style (`.cfg`) file emitted by `dex2oat`, containing 72control-flow graphs (CFGs) for compiled methods at each step (pass) in the 73compiler's pipeline, as well as the emitted assembly code. 74 75## ART gtests 76 77ART gtests are written in C++ using the [Google 78Test](https://github.com/google/googletest) framework. These tests exercise 79various aspects of the runtime (the logic in `libart`, `libart-compiler`, etc.) 80and its binaries (`dalvikvm`, `dex2oat`, `oatdump`, etc.). Some of them are used 81as unit tests to verify a particular construct in ART. These tests may depend on 82some test Dex files and core images. 83 84ART gtests are defined in various directories within the ART project (usually in 85the same directory as the code they exercise). Their source files usually end 86with the suffix `_test.cc`. The construction logic of these tests is implemented 87in ART's build system (`Android.bp` and `Android*.mk` files). On host, these 88gtests can be run by executing `m test-art-host-gtest`. On device, the 89recommended approach is to run these tests in a chroot environment (see 90`README.chroot.md` in this directory). 91 92 93# Test execution 94 95All tests in either suite can be run using the `art/test.py` 96script. Additionally, run-tests can be run individually. All of the tests can be 97run on the build host, on a USB-attached device, or using the build host 98"reference implementation". 99 100ART also supports running target (device) tests in a chroot environment (see 101`README.chroot.md` in this directory). This is currently the recommended way to 102run tests on target (rather than using `art/test.py --target`). 103 104To see command flags run: 105 106```sh 107$ art/test.py -h 108``` 109 110## Building tests 111 112In general all tests require some dependencies to be built before they can be run. 113In general you can pass the `--build-dependencies` flag (also available as short 114option -b) to `art/test.py` program to automatically build required dependencies. 115One can also directly use the various `test-art-...-dependencies` targets listed 116below. 117 118## Running all tests on the build host 119 120```sh 121$ # Build test files 122$ m test-art-host-run-test-dependencies 123$ # Run the tests 124$ art/test.py --host 125``` 126 127Or: 128 129``` 130$ art/test.py -b --host 131``` 132 133## Running all tests on the target device 134 135```sh 136$ # Build test files 137$ m test-art-target-run-test-dependencies 138$ # Run the tests 139$ art/test.py --target 140``` 141 142Or: 143 144``` 145$ art/test.py -b --target 146``` 147 148## Running all gtests on the build host 149 150```sh 151$ art/test.py --host -g 152``` 153 154## Running all gtests on the target device 155 156```sh 157$ art/test.py --target -g 158``` 159 160## Running all run-tests on the build host 161 162```sh 163$ # Build test files 164$ m test-art-host-run-test-dependencies 165$ art/test.py --host -r 166``` 167 168Or: 169 170``` 171$ art/test.py -b --host -r 172``` 173 174## Running all run-tests on the target device 175 176```sh 177$ art/test.py --target -r 178``` 179 180## Building and running one run-test on the build host 181 182```sh 183$ # Build test files 184$ m test-art-host-run-test-dependencies 185$ # Run the tests 186$ art/test.py --host -r -t 001-HelloWorld 187``` 188 189Or: 190 191``` 192$ art/test.py -b --host -r -t 001-HelloWorld 193``` 194 195## Building and running one run-test on the target device 196 197```sh 198$ art/test.py --target -b -r -t 001-HelloWorld 199``` 200 201The `-b` option (re)builds the shard for the given test(s) and pushes it to 202device. However the push may not include all necessary dependencies, e.g. test 203`.so` libraries like `libarttest.so`. 204 205## Running one gtest on the build host 206 207```sh 208$ m test-art-host-gtest-art_runtime_tests 209``` 210 211Note: Although this is a build command, it actually builds the test with 212dependencies and runs the test. 213 214If you want to run the test with more options, use the following commands 215instead. Note that you need to run the test with the command above at least once 216before you run the commands below. 217 218```sh 219$ find out/host/ -type f -name art_runtime_tests # Find the path of the test. 220$ out/host/linux-x86/nativetest/art_runtime_tests/art_runtime_tests 221``` 222 223Add "--no_isolate" to run the tests one by one in single process (disable forking). 224Add "--gtest_filter=..." to select specific sub-test(s) to run. 225Prefix by "gdb --args " to run the test in gdb. 226 227# ART Continuous Integration 228 229Both ART run-tests and gtests are run continuously as part of [ART's continuous 230integration](https://ci.chromium.org/p/art/g/luci/console). In addition, two 231other test suites are run continuously on this service: Libcore tests and JDWP 232tests. 233