Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo test --test does not find any tests #7864

Closed
mqudsi opened this issue Feb 5, 2020 · 6 comments · Fixed by #12915
Closed

cargo test --test does not find any tests #7864

mqudsi opened this issue Feb 5, 2020 · 6 comments · Fixed by #12915
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. A-documenting-cargo-itself Area: Cargo's documentation C-enhancement Category: enhancement

Comments

@mqudsi
Copy link

mqudsi commented Feb 5, 2020

Background: I'm improving fish's dynamic shell completions for cargo to do things like have cargo run --bin <TAB> give you a list of available binaries to choose from for tab completion.

It seems that cargo's dynamic test discovery is broken (or never implemented?).

e.g. while cargo bench --bench returns a list of all benchmarks along with the error message:

$ cargo bench --bench
error: "--bench" takes one argument.
Available benches:
    bench

cargo test --test always returns "no tests available" incorrectly:

$ cargo test --test
error: "--test" takes one argument.
No tests available.

while actually running cargo test shows that cargo has no problem enumerating tests:

mqudsi@ZBOOK /m/c/U/m/g/unquote> cargo test
    Finished test [unoptimized + debuginfo] target(s) in 0.12s
     Running target/debug/deps/unquote-d4809e2bcea39ff6

running 18 tests
... <SNIP>
@mqudsi mqudsi added the C-bug Category: bug label Feb 5, 2020
@ehuss
Copy link
Contributor

ehuss commented Feb 5, 2020

Do you have separate tests in your project? The --test flag only works with integration tests in the tests/ folder. If you do, is your project publicly available so I can look?

@mqudsi
Copy link
Author

mqudsi commented Feb 5, 2020

No, the tests are integrated in the project I tested it on.

The output of cargo test:

running 18 tests
test tests::empty_quotes ... ok
test tests::escaped_double_quote ... ok
test tests::escaped_single_quote ... ok
test tests::interpolated_double_quoted ... ok
test tests::leading_whitespace ... ok
test tests::nested_double_single_quote ... ok
test tests::nested_single_double_quote ... ok
test tests::interword_whitespace ... ok
test tests::one_double_quoted_word ... ok
test tests::one_single_quoted_word ... ok
test tests::one_spaced_double_quoted_word ... ok
test tests::one_spaced_single_quoted_word ... ok
test tests::one_word ... ok
test tests::only_whitespace ... ok
test tests::trailing_whitespace ... ok
test tests::two_double_quoted_words ... ok
test tests::two_single_quoted_words ... ok
test tests::two_words ... ok

but a single test can selectively be run via cargo test TEST_NAME:

mqudsi@ZBOOK /m/c/U/m/g/unquote> cargo test trailing_whitespace
    Finished test [unoptimized + debuginfo] target(s) in 0.13s
     Running target/debug/deps/unquote-d4809e2bcea39ff6

running 1 test
test tests::trailing_whitespace ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 17 filtered out

Meaning all the functionality I am seeking exists, but perhaps it is not exposed? How can I get a list of tests (not test files) without running them all?

@ehuss
Copy link
Contributor

ehuss commented Feb 5, 2020

You can run the test executable with the --list option after it has been built, though I'm not sure how stable that output is. When you pass an argument to cargo test, that is handled by the test harness which handles filtering and such. You can run cargo test -- --help to get the help output from the harness to see its options.

Otherwise, there aren't really any easy ways to get the test names.

@ehuss
Copy link
Contributor

ehuss commented Feb 25, 2020

I'll leave this open, though it really seems to cover a few different issues:

  1. Documentation probably doesn't emphasize enough the difference between --test (an integration test), and a #[test] (a single "test" function).
  2. Errors could probably be a little more detailed. For example, bare --test with no integration tests could explain that the flag is for integration tests.
  3. Provide a better way to programmatically access the list of tests. This is more of a request for libtest, but possibly could use some integration with Cargo.

@ehuss ehuss added A-documenting-cargo-itself Area: Cargo's documentation A-diagnostics Area: Error and warning messages generated by Cargo itself. C-enhancement Category: enhancement and removed C-bug Category: bug labels Feb 25, 2020
@epage
Copy link
Contributor

epage commented Nov 3, 2023

Documentation probably doesn't emphasize enough the difference between --test (an integration test), and a #[test] (a single "test" function).

I think the new help output covers this

Target Selection:
      --lib               Test only this package's library unit tests
      --bins              Test all binaries
      --bin [<NAME>]      Test only the specified binary
      --examples          Test all examples
      --example [<NAME>]  Test only the specified example
      --tests             Test all tests
      --test [<NAME>]     Test only the specified test target
      --benches           Test all benches
      --bench [<NAME>]    Test only the specified bench target
      --all-targets       Test all targets (does not include doctests)

@epage
Copy link
Contributor

epage commented Nov 3, 2023

Provide a better way to programmatically access the list of tests. This is more of a request for libtest, but possibly could use some integration with Cargo.

I feel like this would be best left to rust-lang/rust#49359

bors added a commit that referenced this issue Nov 3, 2023
fix(cli): Clarify --test is for targets, not test functions

We already refer to test targets as "test targets" instead of "tests" in `--test` but not `--tests` or in the error output.  This makes it uniformly refer to them as "test targets", making it clearer that these aren't test functions.

Fixes #7864
@bors bors closed this as completed in 76918a0 Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. A-documenting-cargo-itself Area: Cargo's documentation C-enhancement Category: enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants