|
1 | 1 | #![allow(clippy::test_attr_in_doctest)]
|
| 2 | +//! rustest, an advance test harness. |
| 3 | +//! |
| 4 | +//! This crate provides mainly tree macros ([fixture], [test] and [main]) to setup you tests and their dependencies. |
| 5 | +//! |
| 6 | +//! ``` |
| 7 | +//! use rustest::{test, *}; |
| 8 | +//! |
| 9 | +//! #[fixture] |
| 10 | +//! fn SomeInput() -> u32 { |
| 11 | +//! 42 |
| 12 | +//! } |
| 13 | +//! |
| 14 | +//! #[test] |
| 15 | +//! fn test_42(input: SomeInput) { |
| 16 | +//! assert_eq!(*input, 42); |
| 17 | +//! } |
| 18 | +//! |
| 19 | +//! #[main] |
| 20 | +//! fn main() {} |
| 21 | +//! ``` |
| 22 | +//! |
| 23 | +//! # Setup |
| 24 | +//! |
| 25 | +//! Add rustest to your `Cargo.toml` file: |
| 26 | +//! |
| 27 | +//! ```shell |
| 28 | +//! $ cargo add --dev rustest |
| 29 | +//! ``` |
| 30 | +//! |
| 31 | +//! Rustest comes with its own test harness, so you must deactivate the default one in Cargo.toml: |
| 32 | +//! |
| 33 | +//! ```toml |
| 34 | +//! # In Cargo.toml |
| 35 | +//! |
| 36 | +//! [[test]] |
| 37 | +//! name = "test_name" # for a test located at "tests/test_name.rs" |
| 38 | +//! harness = false |
| 39 | +//! |
| 40 | +//! [[test]] |
| 41 | +//! name = "other_test" # for a test located at "tests/other_test.rs" |
| 42 | +//! harness = false |
| 43 | +//! |
| 44 | +//! # For unit test, you also need to deactivate harness for lib |
| 45 | +//! [lib] |
| 46 | +//! harness = false |
| 47 | +//! ``` |
| 48 | +//! |
| 49 | +//! You also need to add a main function in each of your integration tests. To do so add an empty main function and |
| 50 | +//! mark it with `#[rustest::main]` attribute: |
| 51 | +//! |
| 52 | +//! ```rust |
| 53 | +//! #[rustest::main] |
| 54 | +//! fn main () {} |
| 55 | +//! ``` |
| 56 | +//! |
| 57 | +//! 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: |
| 58 | +//! |
| 59 | +//! ```nocompile |
| 60 | +//! #[cfg(test)] |
| 61 | +//! #[rustest::main] |
| 62 | +//! fn main() {} |
| 63 | +//! ``` |
| 64 | +//! |
| 65 | +//! # Feature flags |
| 66 | +//! |
| 67 | +//! * **googletest**: Add support for [googletest](https://crates.io/crates/googletest) matchers. See [Using google test](#using-google-test) section. |
| 68 | +//! |
| 69 | +//! # Using google test |
| 70 | +//! |
| 71 | +//! If feature flag `googletest` is activated, you can use googletest matchers. You don't need to mark you tests with `#[gtest]`. |
| 72 | +//! |
| 73 | +//! ``` |
| 74 | +//! use googletest::prelude::*; |
| 75 | +//! use rustest::{test, *}; |
| 76 | +//! |
| 77 | +//! #[fixture] |
| 78 | +//! fn Value() -> u32 { 2 } |
| 79 | +//! |
| 80 | +//! #[test] |
| 81 | +//! fn succeed(value: Value) { |
| 82 | +//! assert_that!(value, eq(2)); |
| 83 | +//! } |
| 84 | +//! |
| 85 | +//! #[test] |
| 86 | +//! #[xfail] |
| 87 | +//! fn fails_and_panics(value: Value) { |
| 88 | +//! assert_that!(value, eq(4)); |
| 89 | +//! } |
| 90 | +//! |
| 91 | +//! #[test] |
| 92 | +//! #[xfail] |
| 93 | +//! fn two_logged_failures(value: Value) { |
| 94 | +//! expect_that!(value, eq(4)); // Test now failed, but continues executing. |
| 95 | +//! expect_that!(value, eq(5)); // Second failure is also logged. |
| 96 | +//! } |
| 97 | +//! |
| 98 | +//! #[test] |
| 99 | +//! #[xfail] |
| 100 | +//! fn fails_immediately_without_panic(value: Value) -> googletest::Result<()> { |
| 101 | +//! verify_that!(value, eq(4))?; // Test fails and aborts. |
| 102 | +//! verify_that!(value, eq(2))?; // Never executes. |
| 103 | +//! Ok(()) |
| 104 | +//! } |
| 105 | +//! |
| 106 | +//! #[test] |
| 107 | +//! #[xfail] |
| 108 | +//! fn simple_assertion(value: Value) -> googletest::Result<()> { |
| 109 | +//! verify_that!(value, eq(4)) // One can also just return the last assertion. |
| 110 | +//! } |
| 111 | +//! |
| 112 | +//! #[rustest::main] |
| 113 | +//! fn main () {} |
| 114 | +//! ``` |
2 | 115 |
|
3 | 116 | mod fixture;
|
4 | 117 | mod fixture_display;
|
|
0 commit comments