Post build script to generate tests

I'm in a awkward situation where I need to use the crate I'm writing to generate its test files. Those tests are then expected to be run as usual using cargo test. What is the best way to do that using cargo?

I cannot generate the tests using a build.rs script because I need the crate already compiled, and as far as I know there is no post-build script run by cargo (or is it? I haven't found any open PR/RFC about that). I could create a binary whose purpose is to generate the test files and run it manually, but it would pollute my bin folder where I already have a main binary executable, and I don't want to have it installed using cargo install.

For now I'm using an example to build the tests using cargo example, but it is not really ideal. So I am now wondering if someone out there has a better idea...

Tests in the tests/ directory are run after the crate's library and binaries are compiled, so you should be able to depend on them.

Alternatively, could you make a workspace, and run tests from another crate in the workspace?

1 Like

Tests in the tests/ directory are run after the crate's library and binaries are compiled, so you should be able to depend on them.

Yes, but the problem is where do I put the binary that will generate those tests. It must depend on the library.

I guess I could use a workspace, I'm not really familiar with them. Can you declare some local dependencies between the libraries of the workspace?

Yes, you just use path dependencies within the workspace.

Here's one possible approach, that might, or might not work for you.

Write a test-generation as a test, which generates code into the source directory. If the generated code is the same as the code on disc, this tests passes. If the on-disk files differ, the test overwrites them and then fails.

Then, the first cargo test might fail if new tests should be re-generated. The second cargo test will pass. We do something close in rust-analyzer:

https://github.com/matklad/rust-analyzer/blob/660930623eb002cfc56cd2a4e392ad40e8c53696/crates/syntax/src/tests/sourcegen_tests.rs#L11-L43

I like the idea. I actually have two libraries having this problem so I'm glad I asked the question here because from the two proposed solutions I think the workspace one is better for one project and your idea better for the other. Thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.