Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Guides writing and reviewing idiomatic Rust using Apollo GraphQL's best practices handbook: ownership, errors, performance, testing, docs.
78% 96%+17 ptsof test cases passed
Passed 22 of 23 test cases with the skill, and 18 of 23 without it.
3 of 3 safety checks passed
Not used yet
Tests are not just for correctness. They are the first place people look to understand how your code works.
#[test]. Most code editors can compile and run the functions declared under the macro individually or blocks of them.#[cfg(test)]. Also executable in code editors if it contained #[test], it is a good way to mock complicated functions or override traits.In Rust, as in many other languages, tests often show how the functions are meant to be used. If a test is clear and targeted, it's often more helpful than reading the function body, when combined with other tests, they serve as living documentation.
In the unit test name we should see the following:
unit_of_work: which function we are calling. The action that will be executed. This is often the name of the testmodwhere the function is being tested.
#[cfg(test)]
mod test {
mod function_name {
#[test]
fn returns_y_when_x() { ... }
}
}
expected_behavior: the set of assertions that we need to verify that the test works.state_that_the_test_will_check: the general arrangement, or setup, of the specific test case.
#[test]
fn test_add_happy_path() {
assert_eq!(add(2, 2), 4);
}
Alternatively, if your function has too many tests, you can blob them together in a
mod, it makes it easier to read and navigate.
// OPTION 1
#[test]
Skills that do similar work, with their benchmark result.
Next to this one in the catalogue, plus more from the same author.