diff --git a/doc/tutorial/syntax.md b/doc/tutorial/syntax.md index 0bf00616c6f5..d2c36bcb35e8 100644 --- a/doc/tutorial/syntax.md +++ b/doc/tutorial/syntax.md @@ -281,6 +281,8 @@ exists, convert the result of the expression to the given type. ## Attributes + int { x + x } + + #[test] + fn test_twice() { + let i = -100; + while i < 100 { + assert twice(i) == 2 * i; + i += 1; + } + } + +When you compile the program normally, the `test_twice` function will +not be used. To actually run the tests, compile with the `--test` +flag: + + > rustc --lib twice.rs + > ./twice + running 1 tests + test test_twice ... ok + result: ok. 1 passed; 0 failed; 0 ignored + +Or, if we change the file to fail, for example by replacing `x + x` +with `x + 1`: + + running 1 tests + test test_twice ... FAILED + failures: + test_twice + result: FAILED. 0 passed; 1 failed; 0 ignored + +You can pass a command-line argument to a program compiled with +`--test` to run only the tests whose name matches the given string. If +we had, for example, test functions `test_twice`, `test_once_1`, and +`test_once_2`, running our program with `./twice test_once` would run +the latter two, and running it with `./twice test_once_2` would run +only the last. + +To indicate that a test is supposed to fail instead of pass, you can +give it a `#[should_fail]` attribute. + + use std; + + fn divide(a: float, b: float) -> float { + if b == 0f { fail; } + a / b + } + + #[test] + #[should_fail] + fn divide_by_zero() { divide(1f, 0f); } + +To disable a test completely, add an `#[ignore]` attribute. Running a +test runner (the program compiled with `--test`) with an `--ignored` +command-line flag will cause it to also run the tests labelled as +ignored. + +A program compiled as a test runner will have the configuration flag +`test` defined, so that you can add code that won't be included in a +normal compile with the `#[cfg(test)]` attribute (see [conditional +compilation](syntax.md#conditional)).