2023-12-05 02:39:02 -08:00
|
|
|
# Testing
|
|
|
|
|
|
|
|
Rust community typically authors unit tests in a module placed in the same
|
2023-12-31 00:15:07 +01:00
|
|
|
source file as the code being tested. This was covered [earlier](../testing.md)
|
|
|
|
in the course and looks like this:
|
2023-12-05 02:39:02 -08:00
|
|
|
|
|
|
|
```rust
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
2023-12-31 00:15:07 +01:00
|
|
|
fn my_test() {
|
|
|
|
todo!()
|
|
|
|
}
|
2023-12-05 02:39:02 -08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
In Chromium we place unit tests in a separate source file and we continue to
|
|
|
|
follow this practice for Rust --- this makes tests consistently discoverable and
|
|
|
|
helps to avoid rebuilding `.rs` files a second time (in the `test`
|
|
|
|
configuration).
|
|
|
|
|
|
|
|
This results in the following options for testing Rust code in Chromium:
|
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- Native Rust tests (i.e. `#[test]`). Discouraged outside of
|
|
|
|
`//third_party/rust`.
|
|
|
|
- `gtest` tests authored in C++ and exercising Rust via FFI calls. Sufficient
|
2023-12-05 02:39:02 -08:00
|
|
|
when Rust code is just a thin FFI layer and the existing unit tests provide
|
|
|
|
sufficient coverage for the feature.
|
2023-12-31 00:15:07 +01:00
|
|
|
- `gtest` tests authored in Rust and using the crate under test through its
|
|
|
|
public API (using `pub mod for_testing { ... }` if needed). This is the
|
2023-12-05 02:39:02 -08:00
|
|
|
subject of the next few slides.
|
|
|
|
|
|
|
|
<details>
|
|
|
|
|
|
|
|
Mention that native Rust tests of third-party crates should eventually be
|
2023-12-31 00:15:07 +01:00
|
|
|
exercised by Chromium bots. (Such testing is needed rarely --- only after adding
|
|
|
|
or updating third-party crates.)
|
2023-12-05 02:39:02 -08:00
|
|
|
|
|
|
|
Some examples may help illustrate when C++ `gtest` vs Rust `gtest` should be
|
|
|
|
used:
|
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- QR has very little functionality in the first-party Rust layer (it's just a
|
|
|
|
thin FFI glue) and therefore uses the existing C++ unit tests for testing both
|
|
|
|
the C++ and the Rust implementation (parameterizing the tests so they enable
|
|
|
|
or disable Rust using a `ScopedFeatureList`).
|
2023-12-05 02:39:02 -08:00
|
|
|
|
2023-12-31 00:15:07 +01:00
|
|
|
- Hypothetical/WIP PNG integration may need to implement memory-safe
|
|
|
|
implementation of pixel transformations that are provided by `libpng` but
|
|
|
|
missing in the `png` crate - e.g. RGBA => BGRA, or gamma correction. Such
|
|
|
|
functionality may benefit from separate tests authored in Rust.
|
2023-12-05 02:39:02 -08:00
|
|
|
|
|
|
|
</details>
|