You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Macros to help auto-generate tests based on files.
4
+
5
+
## Usage
6
+
7
+
Pass a glob pattern that'll identify your files and a test-function that'll run for each file. The glob pattern has to start at the root of your crate.
8
+
9
+
You can add a `.expected.` file next to your test file. Its path will be passed to your test function so you can make outcome-based assertions. (Alternatively, write snapshot tests.)
10
+
11
+
Given the following file structure:
12
+
13
+
```txt
14
+
crate/
15
+
|-- src/
16
+
|-- tests/
17
+
|-- queries/
18
+
|-- test.sql
19
+
|-- test.expected.sql
20
+
|-- querytest.rs
21
+
```
22
+
23
+
You can generate tests like so:
24
+
25
+
```rust
26
+
// crate/tests/querytest.rs
27
+
28
+
tests_macros::gen_tests!{
29
+
"tests/queries/*.sql",
30
+
crate::run_test// use `crate::` if the linter complains.
31
+
}
32
+
33
+
fnrun_test(
34
+
test_path:&str, // absolute path on the machine
35
+
expected_path:&str, // absolute path of .expected file
36
+
test_dir:&str// absolute path of the test file's parent
37
+
) {
38
+
// your logic
39
+
}
40
+
```
41
+
42
+
Given a `crate/tests/queries/some_test_abc.sql` file, this will generate the following:
This will be replicated for each file matched by the glob pattern.
56
+
57
+
## Pitfalls
58
+
59
+
- If you use a Rust-keyword as a file name, this'll result in invalid syntax for the generated tests.
60
+
- You might get linting errors if your test files aren't snake case.
61
+
- All files of the glob-pattern must (currently) be `.sql` files.
62
+
- The `.expected.sql` file-name will always be passed, even if the file doesn't exist.
63
+
- The macro will wrap your tests in a `mod tests { .. }` module. If you need multiple generations, wrap them in modules like so: `mod some_test { tests_macros::gen_tests! { .. } }`.
0 commit comments