mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-01-24 08:00:24 +08:00
swap order of sections to resolve @carols10cents @jonathandturner's concerns
This commit is contained in:
parent
2f4c61dc06
commit
71b87e03d7
@ -142,34 +142,6 @@ And that's reflected in the summary line:
|
|||||||
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
|
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
|
## Testing equality
|
||||||
|
|
||||||
Rust provides a pair of macros, `assert_eq!` and `assert_ne!`, that compares
|
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
|
## The `ignore` attribute
|
||||||
|
|
||||||
Sometimes a few specific tests can be very time-consuming to execute. These
|
Sometimes a few specific tests can be very time-consuming to execute. These
|
||||||
|
Loading…
Reference in New Issue
Block a user