diff --git a/docs/rust-testing-with-rstest-fixtures.md b/docs/rust-testing-with-rstest-fixtures.md index 0b084a9f..ea95b799 100644 --- a/docs/rust-testing-with-rstest-fixtures.md +++ b/docs/rust-testing-with-rstest-fixtures.md @@ -853,6 +853,16 @@ Good fixture and test organization mirrors good software design principles. As t - **Scope Management (**`#[once]` **vs. Regular):** Make conscious decisions about fixture lifetimes. Use `#[once]` sparingly, only for genuinely expensive, read-only, and safely static resources, being mindful of its "never dropped" nature.12 Prefer regular (per-test) fixtures for test isolation and proper resource management. - **Modularity:** Group related fixtures and tests into modules. This improves navigation and understanding of the test suite. - **Readability:** Utilize features like `#[from]` for renaming 12 and `#[default]` / `#[with]` for configurable fixtures to enhance the clarity of both fixture definitions and their usage in tests. +- **Utility Macros:** The integration tests define a `lines_vec!` macro for + quickly building `Vec` from string slices. Use it in fixtures to + avoid repetitive `.to_string()` calls. + +```rust +#[fixture] +fn example_table() -> Vec { + lines_vec!("a", "b", "c") +} +``` General testing advice, such as keeping tests small and focused and mocking external dependencies 17, also applies and is well-supported by `rstest`'s design. diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 00000000..db503b0b --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,7 @@ +/// Utility helpers shared across integration tests. + +macro_rules! lines_vec { + ($($line:expr),* $(,)?) => { + vec![$($line.to_string()),*] + }; +} diff --git a/tests/integration.rs b/tests/integration.rs index b54b8edb..1c21a5e9 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -5,6 +5,9 @@ use std::fs::File; use std::io::Write; use tempfile::tempdir; +#[macro_use] +mod common; + #[fixture] /// Provides a sample Markdown table with broken rows for testing purposes. /// @@ -17,10 +20,7 @@ use tempfile::tempdir; /// assert_eq!(table[0], "| A | B | |"); /// ``` fn broken_table() -> Vec { - vec![ - "| A | B | |".to_string(), - "| 1 | 2 | | 3 | 4 |".to_string(), - ] + lines_vec!("| A | B | |", "| 1 | 2 | | 3 | 4 |",) } #[fixture] @@ -35,113 +35,97 @@ fn broken_table() -> Vec { /// assert_eq!(table, vec![String::from("| A | |"), String::from("| 1 | 2 | 3 |")]); /// ``` fn malformed_table() -> Vec { - vec!["| A | |".to_string(), "| 1 | 2 | 3 |".to_string()] + lines_vec!("| A | |", "| 1 | 2 | 3 |") } #[fixture] fn header_table() -> Vec { - vec![ - "| A | B | |".to_string(), - "| --- | --- |".to_string(), - "| 1 | 2 | | 3 | 4 |".to_string(), - ] + lines_vec!("| A | B | |", "| --- | --- |", "| 1 | 2 | | 3 | 4 |",) } #[fixture] fn escaped_pipe_table() -> Vec { - vec![ - "| X | Y | |".to_string(), - "| a \\| b | 1 | | 2 | 3 |".to_string(), - ] + lines_vec!("| X | Y | |", "| a \\| b | 1 | | 2 | 3 |",) } #[fixture] fn indented_table() -> Vec { - vec![ - " | I | J | |".to_string(), - " | 1 | 2 | | 3 | 4 |".to_string(), - ] + lines_vec!(" | I | J | |", " | 1 | 2 | | 3 | 4 |",) } #[fixture] fn html_table() -> Vec { - vec![ - "".to_string(), - "".to_string(), - "".to_string(), - "
AB
12
".to_string(), - ] + lines_vec!( + "", + "", + "", + "
AB
12
", + ) } #[fixture] fn html_table_with_attrs() -> Vec { - vec![ - "".to_string(), - "".to_string(), - "".to_string(), - "
AB
12
".to_string(), - ] + lines_vec!( + "", + "", + "", + "
AB
12
", + ) } #[fixture] fn html_table_with_colspan() -> Vec { - vec![ - "".to_string(), - "".to_string(), - "".to_string(), - "
A
12
".to_string(), - ] + lines_vec!( + "", + "", + "", + "
A
12
", + ) } #[fixture] fn html_table_no_header() -> Vec { - vec![ - "".to_string(), - "".to_string(), - "".to_string(), - "
AB
12
".to_string(), - ] + lines_vec!( + "", + "", + "", + "
AB
12
", + ) } #[fixture] fn html_table_empty() -> Vec { - vec!["
".to_string()] + lines_vec!("
") } #[fixture] fn html_table_unclosed() -> Vec { - vec!["".to_string(), "".to_string()] + lines_vec!("
1
", "") } #[fixture] fn html_table_uppercase() -> Vec { - vec![ - "
1
".to_string(), - "".to_string(), - "".to_string(), - "
AB
12
".to_string(), - ] + lines_vec!( + "", + "", + "", + "
AB
12
", + ) } #[fixture] fn html_table_mixed_case() -> Vec { - vec![ - "".to_string(), - "".to_string(), - "".to_string(), - "
AB
12
".to_string(), - ] + lines_vec!( + "", + "", + "", + "
AB
12
", + ) } #[fixture] fn multiple_tables() -> Vec { - vec![ - "| A | B |".to_string(), - "| 1 | 22 |".to_string(), - String::new(), - "| X | Y |".to_string(), - "| 3 | 4 |".to_string(), - ] + lines_vec!("| A | B |", "| 1 | 22 |", "", "| X | Y |", "| 3 | 4 |",) } #[rstest]