Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

测试属性

以下属性用于指定执行测试的函数。在“test“模式下编译 crate 会启用测试函数的构建以及用于执行测试的测试框架。启用测试模式还会启用 test 条件编译选项

test 属性

test 属性 将一个函数标记为作为测试执行。

Example

#![allow(unused)]
fn main() {
pub fn add(left: u64, right: u64) -> u64 { left + right }
#[test]
fn it_works() {
    let result = add(2, 2);
    assert_eq!(result, 4);
}
}

test 属性使用 MetaWord 语法。

test 属性只能应用于单态的、不接收参数的自由函数,并且其返回类型必须实现 Termination trait。

Note

实现 Termination trait 的一些类型包括:

  • ()
  • Result<T, E> where T: Termination, E: Debug

只有第一次在函数上使用 test 才有效。

Note

rustc 会对第一次之后的使用发出 lint 警告。这可能在将来成为错误。

test 属性从标准库预导入中导出为 std::prelude::v1::test

这些函数仅在测试模式下编译。

Note

测试模式通过向 rustc 传递 --test 参数或使用 cargo test 启用。

测试框架调用返回值的 report 方法,根据结果 ExitCode 是否表示成功终止将测试分类为通过或失败。 特别是:

  • 返回 () 的测试只要终止且不发生 panic 就通过。
  • 返回 Result<(), E> 的测试只要返回 Ok(()) 就通过。
  • 返回 ExitCode::SUCCESS 的测试通过,返回 ExitCode::FAILURE 的测试失败。
  • 不终止的测试既不通不过也不失败。

Example

#![allow(unused)]
fn main() {
use std::io;
fn setup_the_thing() -> io::Result<i32> { Ok(1) }
fn do_the_thing(s: &i32) -> io::Result<()> { Ok(()) }
#[test]
fn test_the_thing() -> io::Result<()> {
    let state = setup_the_thing()?; // 预期成功
    do_the_thing(&state)?;          // 预期成功
    Ok(())
}
}

ignore 属性

ignore 属性 可以与 test 属性一起使用,告知测试框架不要将该函数作为测试执行。

Example

#![allow(unused)]
fn main() {
#[test]
#[ignore]
fn check_thing() {
    // …
}
}

Note

rustc 测试框架支持 --include-ignored 标志来强制运行被忽略的测试。

ignore 属性使用 MetaWordMetaNameValueStr 语法。

ignore 属性的 MetaNameValueStr 形式提供了一种指定测试被忽略原因的方法。

Example

#![allow(unused)]
fn main() {
#[test]
#[ignore = "not yet implemented"]
fn mytest() {
    // …
}
}

ignore 属性只能应用于标注了 test 属性的函数。

Note

rustc 忽略其他位置的用法但会发出 lint 警告。这可能在将来成为错误。

只有第一次在函数上使用 ignore 才有效。

Note

rustc 会对第一次之后的使用发出 lint 警告。这可能在将来成为错误。

被忽略的测试在测试模式下仍会被编译,但不会被运行。

should_panic 属性

should_panic 属性 使测试仅在应用该属性的测试函数发生 panic 时通过。

Example

#![allow(unused)]
fn main() {
#[test]
#[should_panic(expected = "values don't match")]
fn mytest() {
    assert_eq!(1, 2, "values don't match");
}
}

should_panic 属性有以下形式:

  • MetaWord

    Example

    #![allow(unused)]
    fn main() {
    #[test]
    #[should_panic]
    fn mytest() { panic!("error: some message, and more"); }
    }
  • MetaNameValueStr — 给定的字符串必须出现在 panic 消息中,测试才能通过。

    Example

    #![allow(unused)]
    fn main() {
    #[test]
    #[should_panic = "some message"]
    fn mytest() { panic!("error: some message, and more"); }
    }
  • MetaListNameValueStr — 与 MetaNameValueStr 语法一样,给定的字符串必须出现在 panic 消息中。

    Example

    #![allow(unused)]
    fn main() {
    #[test]
    #[should_panic(expected = "some message")]
    fn mytest() { panic!("error: some message, and more"); }
    }

should_panic 属性只能应用于标注了 test 属性的函数。

Note

rustc 忽略其他位置的用法但会发出 lint 警告。这可能在将来成为错误。

只有第一次在函数上使用 should_panic 才有效。

Note

rustc 会对第一次之后的使用发出未来兼容性警告的 lint。这可能在将来成为错误。

当使用 MetaNameValueStr 形式或带有 expected 键的 MetaListNameValueStr 形式时,给定的字符串必须出现在 panic 消息的某处,测试才能通过。

测试函数的返回类型必须是 ()