将特定测试编译为二进制

时间:2017-06-01 03:52:00

标签: unit-testing testing rust rust-cargo

我想编译运行某个测试子集的二进制文件。当我运行以下内容时,它可以工作:

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 named hash :: vec . cargo rustc - --test works, but creates a binary that runs all tests. If I try cargo 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”是什么以及如何传递它以便我得到一个仅在hash::vec中运行指定的9个测试的二进制文件。

1 个答案:

答案 0 :(得分:2)

你不能,至少不是直接。

cargo test hash::vec的情况下,hash::vec只是在执行测试运行时与每个测试函数的完整路径匹配的子字符串。也就是说,它对编译哪些测试完全没有影响,只对哪些测试运行。实际上,这个参数传递给了测试运行器本身;货物本身甚至无法解释它。

对于--test NAMENAME是测试的名称。如同,通过--test blah告诉Cargo在tests/blah.rs中构建和运行测试。它与--bin NAME(对于src/bin/NAME.rs)和--example NAME(对于examples/NAME.rs)具有相同的论点。

如果你真的只想编译一个特定的测试子集,我能想到的唯一方法是通过功能使用条件编译。您希望能够启用/禁用的每个测试子集都需要一个软件包功能。