将特定测试编译成二进制文件

Compile specific tests into binary

我想编译一个包含 运行 特定测试子集的二进制文件。当我 运行 以下内容时,它有效:

ubuntu@ubuntu-xenial:/ox$ cargo test hash::vec
    Finished dev [unoptimized + debuginfo] target(s) in 0.11 secs
     Running target/debug/deps/ox-824a031ff1732165

running 9 tests
test hash::vec::test_hash_entry::test_get_offset_tombstone ... ok
test hash::vec::test_hash_entry::test_get_offset_value ... ok
test hash::vec::test_hash_table::test_delete ... ok
test hash::vec::test_hash_table::test_delete_and_set ... ok
test hash::vec::test_hash_table::test_get_from_hash ... ok
test hash::vec::test_hash_table::test_get_non_existant_from_hash ... ok
test hash::vec::test_hash_table::test_override ... ok
test hash::vec::test_hash_table::test_grow_hash ... ok
test hash::vec::test_hash_table::test_set_after_filled_with_tombstones ... ok

test result: ok. 9 passed; 0 failed; 0 ignored; 0 measured; 8 filtered out

当我尝试 运行 target/debug/deps/ox-824a031ff1732165 时,它 运行 是我所有的测试,而不仅仅是 hash::vec 中指定的 9。

我试过 运行 cargo rustc --test hash::vec 但我明白了 error: no test target namedhash::vec.cargo rustc -- --testworks, but creates a binary that runs all tests. If I trycargo rustc -- --test hash::vec`,我得到:

   Compiling ox v0.1.0 (file:///ox)
error: multiple input filenames provided

error: Could not compile `ox`.

cargo rustc -h 说你可以通过 --test 标志(--test NAME Build only the specified test target)传递 NAME,所以我想知道 "NAME" 是什么以及如何传递它所以我得到一个二进制文件,它只 运行s hash::vec.

中指定的 9 个测试

你不能,至少不能直接。

cargo test hash::vec的情况下,hash::vec只是与每个测试函数的完整路径匹配的子字符串当测试运行ner被执行时。也就是说,它绝对不会影响编译哪些测试,只会影响哪些测试运行。其实这个参数是传递给测试运行ner本身的; Cargo 甚至不会自己解释它。

--test NAME的情况下,NAME是测试的名称source。如同,传递 --test blah 告诉 Cargo 构建和 运行 tests/blah.rs 中的测试。它与 --bin NAME(对于 src/bin/NAME.rs)和 --example NAME(对于 examples/NAME.rs)是同一种参数。

如果你真的只想编译特定的测试子集,我能想到的唯一方法是通过特性使用条件编译。您需要为您希望能够 enable/disable.

的每个测试子集提供一个包功能