From 71b87e03d772ba7a54504e2ab0f83abc8aee6147 Mon Sep 17 00:00:00 2001 From: steveklabnik Date: Mon, 14 Nov 2016 14:37:44 -0500 Subject: [PATCH] swap order of sections to resolve @carols10cents @jonathandturner's concerns --- src/chXX-00-testing.md | 57 +++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/src/chXX-00-testing.md b/src/chXX-00-testing.md index b447801..ef2275b 100644 --- a/src/chXX-00-testing.md +++ b/src/chXX-00-testing.md @@ -142,34 +142,6 @@ And that's reflected in the summary line: test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured ``` -## Inverting failure with `should_panic` - -We can invert our test's failure with another attribute: `should_panic`: - -```rust -#[test] -#[should_panic] -fn it_works() { - assert!(false); -} -``` - -This test will now succeed if we `panic!` and fail if we complete. - -`should_panic` tests can be fragile, as it's hard to guarantee that the test -didn't fail for an unexpected reason. To help with this, an optional `expected` -parameter can be added to the `should_panic` attribute. The test harness will -make sure that the failure message contains the provided text. A safer version -of the example above would be: - -```rust -#[test] -#[should_panic(expected = "assertion failed")] -fn it_works() { - assert!(false); -} -``` - ## Testing equality Rust provides a pair of macros, `assert_eq!` and `assert_ne!`, that compares @@ -223,6 +195,35 @@ fn it_works() { } ``` +## Inverting failure with `should_panic` + +We can invert our test's failure with another attribute: `should_panic`: + +```rust +#[test] +#[should_panic] +fn it_works() { + assert!(false); +} +``` + +This test will now succeed if we `panic!` and fail if we complete. + +`should_panic` tests can be fragile, as it's hard to guarantee that the test +didn't fail for an unexpected reason. To help with this, an optional `expected` +parameter can be added to the `should_panic` attribute. The test harness will +make sure that the failure message contains the provided text. A safer version +of the example above would be: + +```rust +#[test] +#[should_panic(expected = "assertion failed")] +fn it_works() { + assert!(false); +} +``` + + ## The `ignore` attribute Sometimes a few specific tests can be very time-consuming to execute. These