Skip to content

Multiple #[test] attributes run the test multiple times. #90979

New issue

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

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

Already on GitHub? # to your account

Closed
Ethiraric opened this issue Nov 17, 2021 · 4 comments · Fixed by #91172
Closed

Multiple #[test] attributes run the test multiple times. #90979

Ethiraric opened this issue Nov 17, 2021 · 4 comments · Fixed by #91172
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

Comments

@Ethiraric
Copy link
Contributor

If a function has multiple #[test] attributes, the test is run as many times as the attribute is repeated with cargo test.

The following code runs doing_the_thing 8 times:

mod tests {
    #[test]
    #[test]
    #[test]
    #[test]
    #[test]
    #[test]
    #[test]
    #[test]
    fn doing_the_thing() {}
}

fn main() {}

I expected to see this happen: The compiler errors

Instead, this happened: It compiles fine and the test is run 8 times

Meta

rustc --version --verbose:

rustc 1.56.1 (59eed8a2a 2021-11-01)
binary: rustc
commit-hash: 59eed8a2aac0230a8b53e89d4e99d55912ba6b35
commit-date: 2021-11-01
host: x86_64-unknown-linux-gnu
release: 1.56.1
LLVM version: 13.0.0

Happens on nightly as well:

rustc 1.58.0-nightly (d914f17ca 2021-11-16)
binary: rustc
commit-hash: d914f17ca71a33a89b2dc3436fca51b1a091559e
commit-date: 2021-11-16
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0

cc @GuillaumeGomez

@Ethiraric Ethiraric added the C-bug Category: This is a bug. label Nov 17, 2021
@GuillaumeGomez
Copy link
Member

GuillaumeGomez commented Nov 17, 2021

I think we should emit a warning in such case, that shouldn't be too complicated.

@Ethiraric said they would be interested into working on it so I'll assign them and help them implementing it.

@GuillaumeGomez GuillaumeGomez added A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels Nov 17, 2021
@chaitanyav
Copy link
Contributor

@GuillaumeGomez can i work on this one?.

@GuillaumeGomez
Copy link
Member

GuillaumeGomez commented Nov 18, 2021

No, as indicated in my message, @Ethiraric is already working on it.

EDIT: It was a bit rough I think so let me extend my initial message a bit: if you want to contribute, there are other E-easy and E-mentor issues. Don't hesitate to take a look there.

@ehuss
Copy link
Contributor

ehuss commented Nov 18, 2021

FYI, PR #88681 is open to check for duplicate attributes. It doesn't handle test/bench because those are proc-macros, and it can be tricky to detect duplicates on those (see #88681 (comment)). So just a heads up this may not be particularly easy. I'm curious why it doesn't fail this check during subsequent passes.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 16, 2021
…rochenkov

Warn when a `#[test]`-like built-in attribute macro is present multiple times.

Fixes rust-lang#90979.
@bors bors closed this as completed in 7407c4e Dec 17, 2021
TheBlindArchitect pushed a commit to XSLabs/fuchsia that referenced this issue May 23, 2025
#[ip_test] and #[fuchsia::test] both emit #[test], rust allows that and
creates 2 tests with the same name:
rust-lang/rust#90979.

Skip emitting #[test] for #[ip_test] so we correctly only run each test
once.

Change-Id: I6a6a6964d1712721e364051f7af20589c50098e6
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/1284549
Fuchsia-Auto-Submit: Zeling Feng <zeling@google.com>
Reviewed-by: Bruno Dal Bo <brunodalbo@google.com>
Commit-Queue: Auto-Submit <auto-submit-builder@fuchsia-internal-service-accts.iam.gserviceaccount.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants