Organizing Tests

I'd like to organize my tests in a way that cargo only runs the ones in the specified category.

For example

cargo test all  # runs all tests
cargo test unit # runs all unit tests
cargo test it   # runs integration tests
cargo test e2e  # runs end to end tests

There is an section for integration test in the rust by example book but it doesn't say how to run them separately from other tests. Is there a way to do this?

Ok I think i figured it out. I read this section of the rust book Test Organization - The Rust Programming Language.

With the directory structure

  | |
  | |
  | |

and follow the module system, cargo will find the sub tests when i run

cargo test --test integration_tests
  • All tests: cargo test
  • Unit tests in the crate: cargo test --lib for tests in your library or cargo test --bins for any tests in your executable
  • Integration tests (items in the tests/ directory): cargo test --tests
  • end-to-end tests: not really a separate concept in cargo... see the "integration tests" item

You didn't mention this, but you can also execute documentation tests (the code snippets in /// doc-comments) using cargo test --doc.


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.