【问题标题】:Compile specific tests into binary将特定测试编译成二进制
【发布时间】:2017-11-02 01:17:22
【问题描述】:

我想编译一个运行特定测试子集的二进制文件。当我运行以下命令时,它可以工作:

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”是什么以及如何传递它,所以我得到一个只运行指定的二进制文件hash::vec 中的 9 个测试。

【问题讨论】:

    标签: unit-testing testing rust rust-cargo


    【解决方案1】:

    你不能,至少不能直接。

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

    --test NAME 的情况下,NAME 是测试的名称​​来源。如,通过 --test blah 告诉 Cargo 在 tests/blah.rs 中构建和运行测试。这与--bin NAME(对于src/bin/NAME.rs)和--example NAME(对于examples/NAME.rs)的论点相同。

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

    【讨论】:

    • 啊,好吧,这实际上回答了我的问题。我真正需要做的就是运行我的测试子集(我正在尝试运行 valgrind 来检测内存泄漏)。将 hash::vec 传递给二进制文件正是我需要的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多