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 <testname> builds binaries #7958

Closed
alkim0 opened this issue Mar 3, 2020 · 6 comments
Closed

cargo test --test <testname> builds binaries #7958

alkim0 opened this issue Mar 3, 2020 · 6 comments
Labels
A-cargo-targets Area: selection and definition of targets (lib, bins, examples, tests, benches) A-documenting-cargo-itself Area: Cargo's documentation C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@alkim0
Copy link

alkim0 commented Mar 3, 2020

Problem
The documentation for cargo seems to suggest that if I run: cargo test --test apple, then,
this should run test apple without compiling any of the binaries. However, this doesn't seem to be the case.

Steps

  1. Build a new rust project using: cargo new banana.
  2. Create a simple lib.rs file in src/. My contents were:
pub struct A {}
  1. Create a binary directory bin/ in src/ then add a file run.rs; my contents were:
use banana::A;
fn main() {
    let a = A {};
    println!("Hi");
}
  1. Create a tests directory tests/ at the project root. Add a simple test file sample.rs with contents:
use banana::A;
#[test]
fn sample() {
    let a = A {};
    println!("Hi");
}
  1. Run cargo test --test sample
  2. bin/run.rs is compiled. We can tell since we can warnings about unused variables from it.

Notes

Output of cargo version:

cargo 1.43.0-nightly (e57bd0299 2020-02-21)
@alkim0 alkim0 added the C-bug Category: bug label Mar 3, 2020
@ehuss
Copy link
Contributor

ehuss commented Mar 3, 2020

This is done intentionally so that integration tests can execute the binaries to exercise their behavior. Seems like it would be good to document that! In general the Cargo documentation on writing tests is a little shallow.

@alkim0
Copy link
Author

alkim0 commented Mar 3, 2020

Is there a way to build the tests without the binaries?

Also, regarding documentation, according to the Cargo Book, it says:

Passing target selection flags will test only the specified targets.

and under the --tests flag, it says:

By default this includes the library and binaries built as unittests, and integration tests.

implying there is a way to avoid the default behavior, and it also states that --all-targets is equivalent to:

--lib --bins --tests --benches --examples

implying that there is a way to build tests without binaries (since --bins is specifically specified).

I propose these parts be rewritten for clarity.

@ehuss
Copy link
Contributor

ehuss commented Mar 4, 2020

There is not currently a way to skip building the binaries.

@ehuss ehuss added A-documenting-cargo-itself Area: Cargo's documentation C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` A-cargo-targets Area: selection and definition of targets (lib, bins, examples, tests, benches) and removed C-bug Category: bug labels Mar 4, 2020
@weihanglo
Copy link
Member

Seems a explanation for auto-build binaries is already there?

Binary targets are automatically built if there is an integration test or benchmark. This allows an integration test to execute the binary to exercise and test its behavior.

https://github.com/rust-lang/cargo/blob/7b7d80e84ae74dd620c0f294d6f12ada16cdb94e/src/doc/man/cargo-test.md#target-selection

@weihanglo
Copy link
Member

#10726 improves the doc a little. I'll close this issue as resolved. If you feel we need a fix other than a doc update, please file a new issue. Thank you!

@rambip
Copy link

rambip commented Jun 22, 2023

There is not currently a way to skip building the binaries.

Oh, that was exactly the feature I was looking for 😕
I have a Yew project (so it compiles to webassembly, and then generates some javascript glue)
I wanted to write unit tests for this project, but I was not able to make it work:

  • if I run cargo test, cargo will say it cannot run a wasm file
  • if I try to change the target, it will fail to compile parts of the code
  • if I decide to give up the unit tests and write integration tests instead, cargo will still complain it cannot run (if I chose the wasm target) or compile (if I chose the linux target) the tests ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cargo-targets Area: selection and definition of targets (lib, bins, examples, tests, benches) A-documenting-cargo-itself Area: Cargo's documentation C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
Development

No branches or pull requests

4 participants