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