Rust Reference ยท Attributes
The #[ignore] attribute
Reference for Rust #[ignore]: skipping slow or environment-dependent tests, adding a reason, and running ignored tests with cargo test -- --ignored.
#[ignore] placed next to #[test] tells the test harness to skip that test by default. The test still compiles, so it cannot rot silently, and it appears in the output as ignored. You run ignored tests explicitly when you want them.
Use it for tests that are slow, need a network or a database, or depend on something not available on every machine. You can attach a reason string, which the harness prints next to the test name.
#[test] #[ignore = "needs a running database"] fn talks_to_the_database() { /* ... */ }
Try it
As with #[test], the runner compiles with plain rustc, so no harness runs here. The examples show the attribute in context, and how an environment check can do the same job at runtime.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
#[test] #[ignore] fn name() { } #[test] #[ignore = "reason text"] fn name() { }
| Form | Description |
|---|---|
#[ignore] |
Skip by default, no reason shown |
#[ignore = "reason"] |
Skip by default and print the reason |
Command line flags (passed after -- to the test binary):
| Flag | Effect |
|---|---|
--ignored |
Run only the ignored tests |
--include-ignored |
Run ignored and normal tests together |
Edge cases: #[ignore] only has meaning on test functions. It also works on doc tests, through the ```ignore code fence marker, which is a different mechanism from the attribute.
Usage notes
- Running them.
cargo test -- --ignoredruns the skipped ones;cargo test -- --include-ignoredruns everything. - CI. Many projects run normal tests on every commit and ignored tests in a nightly or dedicated job.
- Prefer a reason. The text appears in the report, so teammates know why a test is off.
- Not a substitute for fixing. An ignored test that is broken and forgotten is dead weight; ignore for cost or environment, not to hide failures.
- Conditional skipping. To ignore only on some platforms use
#[cfg_attr(target_os = "windows", ignore)]. - Exit status. Ignored tests do not make the run fail, and the summary line counts them separately.