2

In rust running cargo test runs all (unit, doc, integration, etc) test. Or you can run specific test using filters (2). I don't see a way to run specific groups of test unless all test are in one file.

For example with node.js and mocha you can run test in groups. If I only want to run end-to-end test, I setup a custom mocha-config-e2e.js file and set test directory like spec: 'src/**/test-e2e/*.test.js'. Then do something like "test-e2e": "NODE_ENV=test mocha --config ./mocha-config-test-e2e.js" in package.json. Now when i want to run all end-to-end test, I can run npm run test-e2e from the terminal. I can repeat this for any group I create.

How do I do the same thing in rust? How do I run groups of test by directory or regular expression?

Basically I'm looking to run cargo test-e2e or cargo test-e2e-feature_group1 or something similar where a command runs all test specified by directory or regular expression. This reference shows how to make test in sub-directories run but doesn't reveal how to run specific groups of test.

├── src
├── tests
│   ├── e2e
│   │   ├── feature_group1
│   │   │   ├── example1.rs
│   │   │   ├── example2.rs
│   │   │   ├── example3.rs
│   │   ├── feature_group2
│   │   │   ├── example1.rs
│   │   │   ├── example2.rs
│   │   │   ├── example3.rs
│   ├── integration
│   │   ├── example1.rs
│   │   ├── example1.rs
│   ├── e2e_feature_group1.rs
├── Cargo.toml

The closest I have been able to get is with something like this in tests/e2e_feature_group1.rs:

#[path = "e2e/feature_group1/example1.rs"]
mod example1;
#[path = "e2e/feature_group1/example2.rs"]
mod example2;
...

Then running cargo test --test e2e for all test in tests/e2e/feature_group1 that I add to the e2e_feature_group1.rs file. But that requires creating a tests/[group_name].rs for every group and listing every test that is in the directory instead of using an expression to catch all test in a directory.

Instead it would be nice to call cargo test --regex "tests/e2e/feature_group1/*" or cargo test --config "test_config_e2e1.rs" and put the regex #[path_regex = "e2e/feature_group1/*"] and any other test config I want to run in that file.

jtlindsey
  • 4,346
  • 4
  • 45
  • 73
  • Have you tried the `--test` option? – isaactfa Nov 13 '22 at 14:05
  • If `cargo test e2e::feature_group1` doesn't work for you, could you [edit] to more clearly explain why? I can imagine situations where it might not work, but it looks like it would work for your example. – Dan Getz Nov 13 '22 at 14:06
  • Unless I missed something the ```--test``` option only test a specific file and wouldn't test all groups of test unless all test were in that file. – jtlindsey Nov 13 '22 at 14:08
  • @DanGetz ```cargo test e2e::feature_group1``` didn't work for me but I don't know why and I don't see documentation showing why it should work. Running that code does not execute all test in ```tests/e2e/feature_group1 ```. Even if I put ```test_e2e.rs``` in ```test/``` I'm still limited to running test in one file using ```--test``` or running all test with ```cargo test``` – jtlindsey Nov 13 '22 at 14:44
  • 1
    @jtlindsey ah, sorry, I see I've confused with tests under the `src` directory (organized by module). That works for those, not for tests under `tests` apparently. – Dan Getz Nov 13 '22 at 14:53

0 Answers0