Crate rustest

Source
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

§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§

FixtureCreationError
Represents an error that occurs during the creation of a fixture.
FixtureTeardown
A struct that manages the teardown of a fixture.
Test
An actual test run by rustest
TestContext
The context of a specific test.

Enums§

FixtureScope
Represents the scope of a fixture.
LazyValue
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.
FixtureProxy
A trait representing a Fixture proxy.
ParamName
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.
ToParamName

Functions§

run_tests
Build tests from test_ctors and run them.

Type Aliases§

FixtureCreationResult
The result of a fixture creation.
Result
Result of a test.
TeardownFn
A type alias for a teardown function.
TestGeneratorFn
Function creating a set of Test from a TestContext.

Attribute Macros§

fixture
Define a fixture that you can use in all rustest’s test and fixture arguments.
main
Replace a empty main function into a test harness.
test
test attribute is applied to a test function.