是否可以用 Rust 编写测试,使其不在特定操作系统上运行?

ka *_*a s 4 rust

我正在写一个测试:

#[cfg(test)]
mod tests {
    #[test]
    fn test_something() {
        //content of test function
    }
}
Run Code Online (Sandbox Code Playgroud)

是否可以让这个测试在使用Windows时不运行而只在Linux上运行?

She*_*ter 7

您可以选择根本不编译测试

#[cfg(not(target_os = "windows"))]
#[test]
fn test_something() {
    //content of test function
}
Run Code Online (Sandbox Code Playgroud)

或者您可以选择编译它但不运行它:

#[test]
#[cfg_attr(target_os = "windows", ignore)]
fn test_something() {
    //content of test function
}
Run Code Online (Sandbox Code Playgroud)

也可以看看: