Resources for writing unit tests

Running unit tests

We run unit tests as part of the build process using chromite's cros_run_unit_tests. In a nutshell, this script uses the portage method of building and running unit tests using FEATURES="test". To run all the Chromium OS unit tests, you can run cros_run_unit_tests directly from within your chroot. If you want to run just the unit tests for specific packages, you can use cros_run_unit_tests --packages "space-delimited list of portage package names". See cros_run_unit_tests --help for more information about other options.

As an example, let's say you just want to run the unit tests for the metrics package. To do so, you can run:

Regarding src_test() stanza, it is fine to have them build in the src_compile() stage as well. See also Portage documentation on src_test().

How to Blacklist a package from running its unit tests

It's discouraged to have unit tests that need to be blacklisted. However, if you really need to black list a package from running its unit tests as part of the build, use either RESTRICT="test" in your ebuild or add it to the unit tests blacklist in chromite.lib.portage_util.

Non-native architectures (e.g. QEMU)

Platform2 supports running unittests via qemu for non-native architectures (low level details can be found in this doc). The good news is that the process is the same as described above! Simply use cros_run_unit_tests for your board and specify the packages you want to run.

Caveats

Sometimes qemu is not able to support your unittest due to using functionality not yet implemented. If you see errors like "qemu: unknown syscall: xxx", you'll need to filter that test (see below), and you should file a bug so we can update qemu.

Since qemu only works when run inside the chroot, only ebuilds that use the platform.eclass are supported currently.

Filtering tests in ebuilds

Packages using platform.eclass

Sometimes a test is flaky or requires special consideration before it'll work. You can do this by using the existing gtest filtering logic. For packages using platform.eclass, simply pass it as an argument to platform_test: