Test Modules

This page provides basic information on how to build a rust_test module that uses the Rust test harness.

Writing a basic Rust test

For a live example of an on-device and on-host Rust test, view keystore2 Android.bp, or locate one in many of the crates in the external/rust/crates directory.

A rust_test module builds using rustc's --test flag, which creates tests out of code marked with the #[test] attribute. For more information, see the The Rust Reference Testing Attributes documentation.

Define a test module as follows:

rust_test {
    name: "libfoo_inline_tests",

    // Specify the entry point of your library or binary to run all tests
    // specified in-line with the test attribute.
    srcs: ["src/lib.rs"],

    // Tradefed test suite to include this test in.
    test_suites: ["general-tests"],

    // Autogenerate the test config
    auto_gen_config: true,

    rustlibs: [
        "libfoo",
    ],
}

A TEST_MAPPING file contains a list of tests. Although it’s not a requirement, if you do create a TEST_MAPPING file, the tests you include in it will run in presubmit tests, and can be invoked using atest.

You can reference the TEST_MAPPING documentation for more information, but for the libfoo_inline_tests example, add this to presubmit to enable your test runs on TreeHugger:

{
  "presubmit": [
    {
      "name": "libfoo_inline_tests",
    },
  ]
}

Note that rust_test_host modules run by default in presubmit unless unit_tests: is set to false, so you don't need to declare these in TEST_MAPPING files.

For more information on how the auto_gen_config and test_suites properties work, see the Settings section of the Test Development Workflow documentation.

Notable Rust test properties

The rust_test modules inherit properties from rust_binary modules as described on the Binary Modules page.

The properties defined in the table below are in addition to the Important common properties that apply to all modules. These are either particularly important to Rust test modules, or exhibit unique behavior specific to the rust_test module type.

  • test_harness: Advanced usage, defaults to true.

Set this to false if your rust_test implements its own test harness and you don't need to use the built-in Rust test harness (in other words, setting this to false won't pass the --test flag to rustc).