auto merge of #19560 : sfackler/rust/should-fail-reason, r=alexcrichton
The test harness will make sure that the panic message contains the
specified string. This is useful to help make `#[should_fail]` tests a
bit less brittle by decreasing the chance that the test isn't
"accidentally" passing due to a panic occurring earlier than expected.
The behavior is in some ways similar to JUnit's `expected` feature:
`@Test(expected=NullPointerException.class)`.
Without the message assertion, this test would pass even though it's not
actually reaching the intended part of the code:
```rust
#[test]
#[should_fail(message = "out of bounds")]
fn test_oob_array_access() {
let idx: uint = from_str("13o").unwrap(); // oops, this will panic
[1i32, 2, 3][idx];
}
```
This commit is contained in:
commit
c7a9b49d1b
7 changed files with 165 additions and 29 deletions
22
src/test/run-fail/test-should-fail-bad-message.rs
Normal file
22
src/test/run-fail/test-should-fail-bad-message.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// check-stdout
|
||||
// error-pattern:task 'test_foo' panicked at
|
||||
// compile-flags: --test
|
||||
// ignore-pretty: does not work well with `--test`
|
||||
|
||||
#[test]
|
||||
#[should_fail(expected = "foobar")]
|
||||
fn test_foo() {
|
||||
panic!("blah")
|
||||
}
|
||||
|
||||
|
||||
26
src/test/run-pass/test-should-fail-good-message.rs
Normal file
26
src/test/run-pass/test-should-fail-good-message.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// compile-flags: --test
|
||||
// ignore-pretty: does not work well with `--test`
|
||||
|
||||
#[test]
|
||||
#[should_fail(expected = "foo")]
|
||||
fn test_foo() {
|
||||
panic!("foo bar")
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_fail(expected = "foo")]
|
||||
fn test_foo_dynamic() {
|
||||
panic!("{} bar", "foo")
|
||||
}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue