Module test
Minimal unit test runner
When --cfg test
is provided as an argument to the compiler, this module will override
the main
entrypoint with the test runner which will run all the test cases defined in
the code that is being compiled.
Each test case is executed in a subprocess.
Example
fn add(x: i32, y: i32) -> i32 {
x + y
}
fn sub(x: i32, y: i32) -> i32 {
x - y
}
#[cfg(test)]
mod tests {
#[test]
fn test_add() {
assert_eq!(1.add(2), 3);
}
#[test]
fn test_sub() {
assert_eq!(1.sub(2), -1);
}
#[test(should_fail)]
fn test_panic() {
panic!("oops");
}
}
Run this example
Test attributes
The following attributes are supported: #[test(ignore)]
to skip execution of a test (useful e.g. to make sure
that a test compiles but is not run), #[test(should_fail)]
to mark a test as expected to fail (e.g. through
panic).
Custom test frameworks
Nothing prevents you from writing your own framework, just compile with
--cfg custom_test_framework
and use the test support glue in runtime to get the test
cases.
Modules
Structs
Functions
-
fn should_fail(test: &TestCaseMeta) -> bool
Whether a failure (non-0 exit code) is expected for this test.
-
fn ignored(test: &TestCaseMeta) -> bool
Whether a test should be ignored.
-
fn run(test: &TestCaseMeta) -> TestResult
Run a single test case in a forked process and collect output.
-
-
Parse command line arguments.
-
fn filter_tests(args: Args) -> (Vector<TestCaseMeta>, usize)
Filter tests based on user-provided criteria (e.g. path prefix).
-
Test runner entrypoint.