-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Should examples be unit-testable? #5437
Comments
I personally like the idea of |
I agree with making this opt-in by default, but perhaps it would be nice to have a heuristic that warns if |
Fixes rust-lang#5437 I don't think changing the behavior was quite the correct thing to do. This new behavior is very similar to the old with a few small differences: ``` cargo test ORGINAL: Only builds examples. NEW: Builds all examples. Any example with `test` set is tested. cargo test --tests ORIGINAL: Runs all examples as tests. NEW: Only runs examples as tests if `test` is set. cargo test --examples ORIGINAL: Runs all examples as tests. NEW: (SAME) cargo test --example foo ORIGINAL: Runs the given example as a test. NEW: (SAME) cargo test --all-targets ORIGINAL: Runs all examples as tests. NEW: (SAME) ```
Partially revert change to testing examples. Fixes #5437 I don't think changing the behavior was quite the correct thing to do. This new behavior is very similar to the old with a few small differences: ``` cargo test ORGINAL: Only builds examples. NEW: Builds all examples. Any example with `test` set is tested. cargo test --tests ORIGINAL: Runs all examples as tests. NEW: Only runs examples as tests if `test` is set. cargo test --examples ORIGINAL: Runs all examples as tests. NEW: (SAME) cargo test --example foo ORIGINAL: Runs the given example as a test. NEW: (SAME) cargo test --all-targets ORIGINAL: Runs all examples as tests. NEW: (SAME) ```
As part of #5384, I changed it so that examples aren't unit-tested (to be consistent), but I'm having second thoughts since it is a breaking change (and probably doesn't make complete sense).
If they should be unit-testable, under which circumstances should they be tested? I think the old behavior was a little confusing (sometimes they were tested, sometimes they weren't).
We could treat them like bins (compile twice, once as a test, once regularly), but that would cause
cargo test
to build examples twice for the vast majority of users who don't use this.Another idea is to only compile them as tests if
test = true
is set for the example inCargo.toml
.I'd also be fine with restoring the old behavior.
For reference, here is a list of crates on crates.io that have
#[test]
in an example:The text was updated successfully, but these errors were encountered: