自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
测试模块
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本文将就如何构建使用 Rust 自动化测试框架的 rust_test
模块提供基本信息。
编写基本的 Rust 测试
如需查看设备上和主机上的 Rust 测试实际示例,请查看 keystore2 Android.bp,或者从 external/rust/crates
目录中的许多 crate 中找一个查看。
rust_test
模块使用 rustc 的 --test
标志构建,该标志以标记有 #[test]
属性的代码创建测试。如需了解详情,请参阅 Rust 参考测试属性文档。
按以下方式定义测试模块:
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",
],
}
TEST_MAPPING
文件包含测试列表。虽非强制性要求,但如果您确实创建了 TEST_MAPPING 文件,其中包含的测试将在提交前测试中运行,并可以使用 atest
进行调用。
您可以参考 TEST_MAPPING 文档了解其详情,但对于 libfoo_inline_tests
示例,请将以下代码添加到提交前测试中,使您的测试能够在 TreeHugger 上运行:
{
"presubmit": [
{
"name": "libfoo_inline_tests",
},
]
}
请注意,除非将 unit_tests:
设为 false
,否则默认情况下 rust_test_host
模块会在提交前测试中运行,因此您无需在 TEST_MAPPING
文件中声明这些模块。
如需详细了解 auto_gen_config
和 test_suites
属性的工作方式,请参阅测试开发工作流文档的设置部分。
重要的 Rust 测试属性
rust_test
模块从 rust_binary
模块继承属性,如二进制模块页面中所述。
除了适用于所有模块的重要的通用属性之外,还有下表中定义的一些属性。这些属性要么对 Rust 测试模块特别重要,要么具有特定于 rust_test
模块类型的独有行为。
- test_harness:高级用法,默认设为 true。
如果您的 rust_test
实现了自己的自动化测试框架,您不需要使用内置的 Rust 自动化测试框架,请将此属性设为 false(换言之,将此属性设为 false 就不会将 --test
标志传递给 rustc)。
避免 rust_library 和 rust_test 之间出现重复
通过嵌套模块使用内嵌 Rust 测试时,最终会导致 Android.bp
文件中出现重复。问题在于,您必须为 rust_library
和 rust_test
各列出一次依赖项:
rust_library {
name: "libfoo",
srcs: ["src/lib.rs"],
rustlibs: [
"libx",
"liby",
"libz",
],
}
rust_test {
name: "libfoo_inline_tests",
srcs: ["src/lib.rs"],
test_suites: ["general-tests"],
rustlibs: [
"libx",
"liby",
"libz",
],
}
每个 rust_test
模块最终都会列出与对应的 rust_library
模块相同的依赖项。为确保不同模块之间的一致性,您可以在 rust_defaults
模块中仅列出一次依赖项:
rust_defaults {
name: "libfoo_defaults",
srcs: ["src/lib.rs"],
rustlibs: [
"libx",
"liby",
"libz",
],
}
rust_library {
name: "libfoo",
defaults: ["libfoo_defaults"],
}
rust_test {
name: "libfoo_inline_tests",
defaults: ["libfoo_defaults"],
test_suites: ["general-tests"],
}
这样,库和测试模块将始终使用相同的依赖项。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-04-04。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-04-04。"],[],[],null,["# Test modules\n\nThis page provides basic information on how to build a `rust_test` module\nthat uses the Rust test harness.\n\nWrite a basic Rust test\n-----------------------\n\nFor a live example of an on-device and on-host Rust test, view\n[keystore2 Android.bp](https://android.googlesource.com/platform/system/security/+/2f503e597130e1d65dbf9fb4cd0fbd6a2f9ccb07/keystore2/Android.bp),\nor locate one in many of the crates in the `external/rust/crates` directory.\n\nA `rust_test` module builds using rustc's `--test` flag, which creates tests\nout of code marked with the `#[test]` attribute. For more information, see\nthe [The Rust Reference Testing Attributes](https://doc.rust-lang.org/reference/attributes/testing.html)\ndocumentation.\n\nDefine a test module as follows: \n\n rust_test {\n name: \"libfoo_inline_tests\",\n\n // Specify the entry point of your library or binary to run all tests\n // specified in-line with the test attribute.\n srcs: [\"src/lib.rs\"],\n\n // Tradefed test suite to include this test in.\n test_suites: [\"general-tests\"],\n\n // Autogenerate the test config\n auto_gen_config: true,\n\n rustlibs: [\n \"libfoo\",\n ],\n }\n\nA `TEST_MAPPING` file contains a list of tests. Although it's not a requirement,\nif you do create a TEST_MAPPING file, the tests you include in it will run in\npresubmit tests, and can be invoked using [`atest`](/docs/core/tests/development/atest).\n\nYou can reference the [TEST_MAPPING documentation](/compatibility/tests/development/test-mapping)\nfor more information, but for the `libfoo_inline_tests` example, add this to\npresubmit to enable your test runs on TreeHugger: \n\n {\n \"presubmit\": [\n {\n \"name\": \"libfoo_inline_tests\",\n },\n ]\n }\n\nNote that `rust_test_host` modules run by default in presubmit unless\n`unit_tests:` is set to `false`, so you don't need to declare these\nin `TEST_MAPPING` files.\n\nFor more information on how the `auto_gen_config` and `test_suites` properties work,\nsee the [Settings](/docs/core/tests/development/blueprints#settings) section\nof the [Test Development Workflow](/docs/core/tests/development) documentation.\n\nNotable Rust test properties\n----------------------------\n\nThe `rust_test` modules inherit properties from `rust_binary` modules as described on\nthe [Binary Modules](/docs/setup/build/rust/building-rust-modules/binary-modules)\npage.\n\nThe properties defined in the table below are in addition to the\n[Important common properties](/docs/setup/build/rust/building-rust-modules/android-rust-modules#important-common-properties)\nthat apply to all modules. These are either particularly important to Rust\ntest modules, or exhibit unique behavior specific to the `rust_test` module type.\n\n- **test_harness**: Advanced usage, defaults to true.\n\nSet this to false if your `rust_test` implements its own test harness and you don't\nneed to use the built-in Rust test harness (in other words, setting this to false\n*won't* pass the `--test` flag to rustc).\n\nAvoid duplication between rust_library and rust_test\n----------------------------------------------------\n\nWhen you use inline Rust tests via nested modules, you end up with duplication\nin your `Android.bp` file. The problem is that you have to list the dependencies\ntwice, once for `rust_library` and once for `rust_test`: \n\n rust_library {\n name: \"libfoo\",\n srcs: [\"src/lib.rs\"],\n rustlibs: [\n \"libx\",\n \"liby\",\n \"libz\",\n ],\n }\n\n rust_test {\n name: \"libfoo_inline_tests\",\n srcs: [\"src/lib.rs\"],\n test_suites: [\"general-tests\"],\n rustlibs: [\n \"libx\",\n \"liby\",\n \"libz\",\n ],\n }\n\nEach `rust_test` module will end up listing the same dependencies as the\ncorresponding `rust_library` module. To ensure consistency between the modules,\nyou can list the dependencies just once in a `rust_defaults` module: \n\n rust_defaults {\n name: \"libfoo_defaults\",\n srcs: [\"src/lib.rs\"],\n rustlibs: [\n \"libx\",\n \"liby\",\n \"libz\",\n ],\n }\n\n rust_library {\n name: \"libfoo\",\n defaults: [\"libfoo_defaults\"],\n }\n\n rust_test {\n name: \"libfoo_inline_tests\",\n defaults: [\"libfoo_defaults\"],\n test_suites: [\"general-tests\"],\n }\n\nThis way, the library and the test module will always use the same dependencies."]]