Hi. I have an API server that I want to re-write to Rust & Actix-web. Since this project is rather large, I splice it up to several sub-projects (libraries). The main.rs acts as the root of the application.
Each of this library has its own test. I can use terminal and use cd command to each sub-project and run the test on it with cargo r
But it's kind a tedious for me, so my question is can I run test on all of them at once (ideally from the top / root project folder) ?
Note that there are two ways to arrange a workspace: one with a root package (where your main currently is) and one with a “virtual manifest” where the top level isn't a package at all. I recommend the second option since it causes Cargo commands to operate on the whole workspace by default.
(Also, packages in a workspace are not usually put in the src directory, but at the top level or in a directory called crates sometimes.)
Basically your solution at that time is to publicly publish my libraries to crates.io, and add the dependencies to it just like any other packages.
But this is not an ideal solution for me, not because I'm selfish but it's a private code, so I cannot publish them for everyone to see. So I work around that and just create another subproject inside.
If you have one package, then cargo test should automatically run all tests in it; no extra steps are required.
If you have multiple packages, then creating a workspace won't require you to publish anything.
Wait — you said
Each of this library has its own test. I can use terminal and use cd command to each sub-project and run the test on it with cargo r
If you're using cargo run and not cargo test, that sounds like your "tests" are not defined as cargo test targets, which is a problem because it means you can't run them all. But it also sounds like you have something odd going on, because cding within a package doesn't change anything.
Could you show what your project layout within those directories is? And what is in the Cargo.toml file(s)? I suspect that something non-standard is going on and we will need details to give good advice.
Okay, you have many packages. If you want to run tests on all of them, you need to create a workspace — see the links in my first post in this thread. Then you can run cargo test --workspace to run all tests.
Usually subcrates are in folders at the top level (or in a folder structure of their own), not in folders inside src. I don't think it makes a huge difference but it is a bit odd to have subcrates in the src folder.
I experimented a bit with a similar folder structure and I think the problem is that cargo test is defaulting to just testing the top level crate.