Expand description
rustest, an advance test harness.
This crate provides mainly three macros (fixture, test and main) to set up your tests and their dependencies.
use rustest::{test, *};
#[fixture]
fn SomeInput() -> u32 {
42
}
#[test]
fn test_42(input: SomeInput) {
assert_eq!(*input, 42);
}
#[main]
fn main() {}
§Setup
Add rustest to your Cargo.toml
file:
$ cargo add --dev rustest
Rustest comes with its own test harness, so you must deactivate the default one in Cargo.toml:
# In Cargo.toml
[[test]]
name = "test_name" # for a test located at "tests/test_name.rs"
harness = false
[[test]]
name = "other_test" # for a test located at "tests/other_test.rs"
harness = false
# For unit test, you also need to deactivate harness for lib
[lib]
harness = false
You also need to add a main function in each of your integration tests. To do so add an empty main function and
mark it with #[rustest::main]
attribute:
#[rustest::main]
fn main () {}
For unit testing, add the main function at end of you lib.rs
file, but add a cfg(test)
to add it only for tests:
#[cfg(test)]
#[rustest::main]
fn main() {}
§Feature flags
- googletest: Add support for googletest matchers. See Using google test section.
§Using google test
If feature flag googletest
is activated, you can use googletest matchers. You don’t need to mark you tests with #[gtest]
.
use googletest::prelude::*;
use rustest::{test, *};
#[fixture]
fn Value() -> u32 { 2 }
#[test]
fn succeed(value: Value) {
assert_that!(*value, eq(2));
}
#[test]
#[xfail]
fn fails_and_panics(value: Value) {
assert_that!(*value, eq(4));
}
#[test]
#[xfail]
fn two_logged_failures(value: Value) {
expect_that!(*value, eq(4)); // Test now failed, but continues executing.
expect_that!(*value, eq(5)); // Second failure is also logged.
}
#[test]
#[xfail]
fn fails_immediately_without_panic(value: Value) -> googletest::Result<()> {
verify_that!(*value, eq(4))?; // Test fails and aborts.
verify_that!(*value, eq(2))?; // Never executes.
Ok(())
}
#[test]
#[xfail]
fn simple_assertion(value: Value) -> googletest::Result<()> {
verify_that!(*value, eq(4)) // One can also just return the last assertion.
}
#[rustest::main]
fn main () {}
Structs§
- Fixture
Creation Error - Represents an error that occurs during the creation of a fixture.
- Fixture
Teardown - A struct that manages the teardown of a fixture.
- Test
- An actual test run by rustest
- Test
Context - The context of a specific test.
Enums§
- Fixture
Scope - Represents the scope of a fixture.
- Lazy
Value - A lazyness value build when we get it. The value is build by calling the Fn (in get) using the builders. This intermediate structure is needed as we know the sub builders when we setup a builder, and we have the build fn when we build the fixture.
Traits§
- Duplicate
- Duplicate trait is really closed to Clone trait but with slightly sementic difference.
- Fixture
- A trait representing a fixture that can be set up and torn down.
- Fixture
Proxy - A trait representing a Fixture proxy.
- Param
Name - A trait to get the name of a param when we have multiple combination.
- SubFixture
- A fixture that can be used as dependency for another fixture.
- ToParam
Name
Functions§
- run_
tests - Build tests from
test_ctors
and run them.
Type Aliases§
- Fixture
Creation Result - The result of a fixture creation.
- Result
- Result of a test.
- Teardown
Fn - A type alias for a teardown function.
- Test
Generator Fn - Function creating a set of Test from a TestContext.