src/testing/other.md
If you want to test your library as a client, use an integration test.
Create a .rs file under tests/:
# // Copyright 2023 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
// tests/my_library.rs
use my_library::init;
#[test]
fn test_init() {
assert!(init().is_ok());
}
These tests only have access to the public API of your crate.
Rust has built-in support for documentation tests:
# // Copyright 2023 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
/// Shortens a string to the given length.
///
/// ```
/// # use playground::shorten_string;
/// assert_eq!(shorten_string("Hello World", 5), "Hello");
/// assert_eq!(shorten_string("Hello World", 20), "Hello World");
/// ```
pub fn shorten_string(s: &str, length: usize) -> &str {
&s[..std::cmp::min(length, s.len())]
}
/// comments are automatically seen as Rust code.cargo test.# in the code will hide it from the docs, but will still compile/run
it.