1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-12-26 11:17:49 +02:00

Move Mockall and GoogleTest slides to Android section (#1533)

After #1528 and #1532, we now have actual slides which showcase the
crates in action. So we can reclaim a few minutes by removing the slide
which mentions Mockall and GoogleTest slide.

The slide mentioned [proptest](https://docs.rs/proptest) and
[rstest](https://docs.rs/rstest) as well. While I'm sure the libraries
are useful, we don't have them imported into AOSP and I've never
personally used them. We should therefore not advertise them yet at this
point since they won't be useful to Android engineers.

Of course we can mention things that are not in AOSP (or in Chromium),
but I think we should do it in the speaker notes at most.
This commit is contained in:
Martin Geisler 2024-03-04 16:25:35 +01:00 committed by GitHub
parent c763932288
commit 1b3984df20
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 129 additions and 43 deletions

12
Cargo.lock generated
View File

@ -59,6 +59,14 @@ dependencies = [
"url", "url",
] ]
[[package]]
name = "android-testing"
version = "0.1.0"
dependencies = [
"googletest",
"mockall",
]
[[package]] [[package]]
name = "android-tzdata" name = "android-tzdata"
version = "0.1.1" version = "0.1.1"
@ -2372,10 +2380,6 @@ checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76"
[[package]] [[package]]
name = "testing" name = "testing"
version = "0.1.0" version = "0.1.0"
dependencies = [
"googletest",
"mockall",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"

View File

@ -3,6 +3,7 @@
members = [ members = [
"mdbook-course", "mdbook-course",
"mdbook-exerciser", "mdbook-exerciser",
"src/android/testing",
"src/bare-metal/useful-crates/allocator-example", "src/bare-metal/useful-crates/allocator-example",
"src/bare-metal/useful-crates/zerocopy-example", "src/bare-metal/useful-crates/zerocopy-example",
"src/borrowing", "src/borrowing",

View File

@ -197,7 +197,10 @@ use-boolean-and = true
"structs/tuple-structs.html" = "../user-defined-types/tuple-structs.html" "structs/tuple-structs.html" = "../user-defined-types/tuple-structs.html"
"structure.html" = "running-the-course/course-structure.html" "structure.html" = "running-the-course/course-structure.html"
"testing/doc-tests.html" = "../testing/other.html" "testing/doc-tests.html" = "../testing/other.html"
"testing/googletest.html" = "../android/testing/googletest.html"
"testing/integration-tests.html" = "../testing/other.html" "testing/integration-tests.html" = "../testing/other.html"
"testing/mockall.html" = "../android/testing/mockall.html"
"testing/useful-crates.html" = "../testing.html"
"traits.html" = "methods-and-traits/traits.html" "traits.html" = "methods-and-traits/traits.html"
"traits/closures.html" = "../std-traits/closures.html" "traits/closures.html" = "../std-traits/closures.html"
"traits/default-methods.html" = "../methods-and-traits/traits.html" "traits/default-methods.html" = "../methods-and-traits/traits.html"

View File

@ -45,7 +45,7 @@ fn timediff(actual: u64, target: u64, slop: u64) -> String {
duration(actual), duration(actual),
duration(actual - target), duration(actual - target),
) )
} else if actual < target - slop { } else if actual + slop < target {
format!("{}: ({} short)", duration(actual), duration(target - actual),) format!("{}: ({} short)", duration(actual), duration(target - actual),)
} else { } else {
format!("{}", duration(actual)) format!("{}", duration(actual))

View File

@ -178,9 +178,6 @@
- [Testing](testing.md) - [Testing](testing.md)
- [Test Modules](testing/unit-tests.md) - [Test Modules](testing/unit-tests.md)
- [Other Types of Tests](testing/other.md) - [Other Types of Tests](testing/other.md)
- [Useful Crates](testing/useful-crates.md)
- [GoogleTest](testing/googletest.md)
- [Mocking](testing/mocking.md)
- [Compiler Lints and Clippy](testing/lints.md) - [Compiler Lints and Clippy](testing/lints.md)
- [Exercise: Luhn Algorithm](testing/exercise.md) - [Exercise: Luhn Algorithm](testing/exercise.md)
- [Solution](testing/solution.md) - [Solution](testing/solution.md)
@ -231,6 +228,9 @@
- [Sending Objects](android/aidl/types/objects.md) - [Sending Objects](android/aidl/types/objects.md)
- [Parcelables](android/aidl/types/parcelables.md) - [Parcelables](android/aidl/types/parcelables.md)
- [Sending Files](android/aidl/types/file-descriptor.md) - [Sending Files](android/aidl/types/file-descriptor.md)
- [Testing](android/testing.md)
- [GoogleTest](android/testing/googletest.md)
- [Mocking](android/testing/mocking.md)
- [Logging](android/logging.md) - [Logging](android/logging.md)
- [Interoperability](android/interoperability.md) - [Interoperability](android/interoperability.md)
- [With C](android/interoperability/with-c.md) - [With C](android/interoperability/with-c.md)

View File

@ -117,6 +117,11 @@ EOF
pkill -f birthday_server pkill -f birthday_server
run_example <<EOF
# ANCHOR: libleftpad_test
atest --host libleftpad_test
# ANCHOR_END: libleftpad_test
EOF
run_example <<EOF run_example <<EOF
# ANCHOR: hello_rust_logs # ANCHOR: hello_rust_logs

37
src/android/testing.md Normal file
View File

@ -0,0 +1,37 @@
# Testing in Android
Building on [Testing](../testing.md), we will now look at how unit tests work in
AOSP. Use the `rust_test` module for your unit tests:
_testing/Android.bp_:
```javascript
{{#include testing/Android.bp}}
```
_testing/src/lib.rs_:
```rust
{{#include testing/src/lib.rs:leftpad}}
```
You can now run the test with
```shell
{{#include build_all.sh:libleftpad_test}}
```
The output looks like this:
```text
INFO: Elapsed time: 2.666s, Critical Path: 2.40s
INFO: 3 processes: 2 internal, 1 linux-sandbox.
INFO: Build completed successfully, 3 total actions
//comprehensive-rust-android/testing:libleftpad_test_host PASSED in 2.3s
PASSED libleftpad_test.tests::long_string (0.0s)
PASSED libleftpad_test.tests::short_string (0.0s)
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.

View File

@ -0,0 +1,13 @@
rust_library {
name: "libleftpad",
crate_name: "leftpad",
srcs: ["src/lib.rs"],
}
rust_test {
name: "libleftpad_test",
crate_name: "leftpad_test",
srcs: ["src/lib.rs"],
host_supported: true,
test_suites: ["general-tests"],
}

View File

@ -0,0 +1,21 @@
[package]
name = "android-testing"
version = "0.1.0"
edition = "2021"
publish = false
[[example]]
name = "googletest-example"
crate-type = ["staticlib"]
path = "googletest.rs"
test = true
[[example]]
name = "mockall-example"
crate-type = ["staticlib"]
path = "mockall.rs"
test = true
[dependencies]
googletest = "0.11.0"
mockall = "0.12.1"

View File

@ -69,6 +69,4 @@ Difference(-actual / +expected):
[prelude]: https://docs.rs/googletest/latest/googletest/prelude/index.html [prelude]: https://docs.rs/googletest/latest/googletest/prelude/index.html
- GoogleTest is available for use in AOSP.
</details> </details>

View File

@ -15,8 +15,7 @@ to use traits, which you can then quickly mock:
<details> <details>
- The advice here is for Android (AOSP) where Mockall is the recommended mocking - Mockall is the recommended mocking library in Android (AOSP). There are other
library. There are other
[mocking libraries available on crates.io](https://crates.io/keywords/mock), [mocking libraries available on crates.io](https://crates.io/keywords/mock),
in particular in the area of mocking HTTP services. The other mocking in particular in the area of mocking HTTP services. The other mocking
libraries work in a similar fashion as Mockall, meaning that they make it easy libraries work in a similar fashion as Mockall, meaning that they make it easy

View File

@ -0,0 +1,36 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ANCHOR: leftpad
//! Left-padding library.
/// Left-pad `s` to `width`.
pub fn leftpad(s: &str, width: usize) -> String {
format!("{s:>width$}")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn short_string() {
assert_eq!(leftpad("foo", 5), " foo");
}
#[test]
fn long_string() {
assert_eq!(leftpad("foobar", 6), "foobar");
}
}

View File

@ -4,22 +4,6 @@ version = "0.1.0"
edition = "2021" edition = "2021"
publish = false publish = false
[[example]]
name = "googletest-example"
crate-type = ["staticlib"]
path = "googletest.rs"
test = true
[[example]]
name = "mockall-example"
crate-type = ["staticlib"]
path = "mockall.rs"
test = true
[[bin]] [[bin]]
name = "luhn" name = "luhn"
path = "exercise.rs" path = "exercise.rs"
[dependencies]
googletest = "0.11.0"
mockall = "0.12.1"

View File

@ -1,15 +0,0 @@
---
minutes: 3
---
# Useful Crates
Rust comes with only basic support for writing tests.
Here are some additional crates which we recommend for writing tests:
- [googletest](https://docs.rs/googletest): Comprehensive test assertion library
in the tradition of GoogleTest for C++.
- [proptest](https://docs.rs/proptest): Property-based testing for Rust.
- [rstest](https://docs.rs/rstest): Support for fixtures and parameterised
tests.