Running `cargo test` without cargo

Hi everyone,

i am trying to figure out if there is a way to run all tests in a project without using cargo. So I basically want to replicate what cargo test does but without relying on cargo. It it ends up requiring some nasty bash scripting I could live with that. Right now i have absolutely no idea however.

Thanks in advance!
-Tobi

PS: In case you are wondering why the heck i would want to do something weird like that: I am trying to to execute tests as part of a Nix Rust build function which doesn't use cargo directly.. I prefer to not derail into the details of that :sweat_smile:

There are 3 kinds of tests that cargo test runs:

  1. Unit tests in the main source
  2. Functional tests in the tests folder
  3. Doc tests in the main source

The should all be relatively easy if you have the library compiling, you just need to change the flags slightly. Given a crate that is compiled with a command line like

> rustc --crate-name foo --crate-type lib src/lib.rs

then compiling the unit tests should be something like

> rustc --crate-name foo src/lib.rs --test

That will give you a binary that can be run to run the tests.

Similarly, once you have compiled the library you can compile and link the functional tests against it

> rustc --crate-name bar tests/bar.rs --test --extern foo=libfoo.rlib
> rustc --crate-name baz tests/baz.rs --test --extern foo=libfoo.rlib

These will again build binaries that will run the tests when executed.

Finally, to run doc tests you need to use rustdoc and link the compiled library in

> rustdoc --crate-name foo src/lib.rs --test --extern foo=libfoo.rlib

This will compile and run each of the doc-tests itself, not producing a binary like the other commands.

With each of these you will of course also need to be adding in all the necessary dev-dependencies, but that should be pretty straightforward if you have dependency management for the main library working.

6 Likes

Thank you so much for your detailed response. Very helpful. I'll give it a try :wink:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.