Expand description
An extended Rust testcase
The testx
crate provides the testx
macro, which is an extended version
of the Rust test
macro. The key features are:
- The
testx
macro is fully compatible for the Rusttest
macro, all tests maked with#[testx]
(instead of#[test]
) are executed withcargo-test
. - Support for a test preparation function.
To use it, put the following line into the [dev-dependencies]
section of
your Cargo.toml
:
[dev-dependencies]
testx = "0.1.2"
Create a testx
testcase
Mark the testcase with #[testx]
. Calling cargo test
will execute the
testcase.
use testx::testx;
#[testx]
fn sample() {
assert_eq!(1, 1);
}
// output:
// running 1 test
// test sample ... ok
Migrate from #[test]
to #[testx]
Simply replace the line #[test]
with #[testx]
for those tests you want
migrate.
Prepare data for a testcase with a setup
function
Provide a function setup
which prepares and returns some data for your
testcase. Next, your testcase needs one argument, which must match the
return value of the setup function.
A testcase marked with #[testx]
will first execute the setup
function
and will pass its return value to your testcase.
use testx::testx;
fn setup() -> u32 {
4711
}
#[testx]
fn sample(num: u32) {
assert_eq!(num, 4711);
}
If more than one argument is required, the setup
function should collect
them into a tuple. The testx
macro will disassemble them into several
arguments.
use testx::testx;
fn setup() -> (u32, String) {
(4711, String::from("foo"))
}
#[testx]
fn sample(num: u32, str: String) {
assert_eq!(num, 4711);
assert_eq!(str, "foo");
}
Note: For a testcase without an argument, the setup
function will not
be executed!
By default a function called setup
is called for each testcase. If you
need another function, assign a setup
attribute with the name of the
setup function to the macro. The function name can be either a string or
the path to the function. Assign the no_setup
attribute if you want to
explicity mark a testcase to have no setup function.
use testx::testx;
fn setup_666() -> u32 {
666
}
#[testx(no_setup)]
fn sample_no_setup() {
assert_eq!(1, 1);
}
#[testx(setup = "setup_666")]
fn sample_custom_str(num: u32) {
assert_eq!(num, 666);
}
#[testx(setup = setup_666)]
fn sample_custom_path(num: u32) {
assert_eq!(num, 666);
}
#[testx(setup = self::setup_666)]
fn sample_custom_path2(num: u32) {
assert_eq!(num, 666);
}