Testing in Android
Building on Testing, we will now look at how unit tests work in AOSP. Use the rust_test module for your unit tests:
testing/Android.bp:
1. rust_library {
2. name: "libleftpad",
3. crate_name: "leftpad",
4. srcs: ["src/lib.rs"],
5. }
7. rust_test {
8. name: "libleftpad_test",
9. crate_name: "leftpad_test",
10. srcs: ["src/lib.rs"],
11. host_supported: true,
12. test_suites: ["general-tests"],
13. }
testing/src/lib.rs:
1. #![allow(unused)]
2. fn main() {
3. //! Left-padding library.
5. /// Left-pad `s` to `width`.
6. pub fn leftpad(s: &str, width: usize) -> String {
7. format!("{s:>width$}")
8. }
10. #[cfg(test)]
11. mod tests {
12. use super::*;
14. #[test]
15. fn short_string() {
16. assert_eq!(leftpad("foo", 5), " foo");
17. }
19. #[test]
20. fn long_string() {
21. assert_eq!(leftpad("foobar", 6), "foobar");
22. }
23. }
24. }
You can now run the test with
1. atest --host libleftpad_test
The output looks like this:
1. INFO: Elapsed time: 2.666s, Critical Path: 2.40s
2. INFO: 3 processes: 2 internal, 1 linux-sandbox.
3. INFO: Build completed successfully, 3 total actions
4. //comprehensive-rust-android/testing:libleftpad_test_host PASSED in 2.3s
5. PASSED libleftpad_test.tests::long_string (0.0s)
6. PASSED libleftpad_test.tests::short_string (0.0s)
7. Test cases: finished with 2 passing and 0 failing out of 2 test cases
Notice how you only mention the root of the library crate. Tests are found recursively in nested modules.
