简体   繁体   中英

Compile specific tests into binary

I would like to compile a binary which runs a certain subset of tests. When I run the following, it works:

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

When I try to run target/debug/deps/ox-824a031ff1732165 , it runs all my tests, not just the 9 specified in hash::vec .

I've tried to run cargo rustc --test hash::vec but I get error: no test target named hash::vec . cargo rustc -- --test works, but creates a binary that runs all tests. If I try works, but creates a binary that runs all tests. If I try cargo rustc -- --test hash::vec`, I get:

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

error: Could not compile `ox`.

cargo rustc -h says that you can pass NAME with the --test flag ( --test NAME Build only the specified test target ), so I'm wondering what "NAME" is and how to pass it in so I get a binary that only runs the specified 9 tests in hash::vec .

You can't, at least not directly.

In the case of cargo test hash::vec , the hash::vec is just a substring matched against the full path of each test function when the test runner is executed . That is, it has absolutely no impact whatsoever on which tests get compiled, only on which tests run. In fact, this parameter is passed to the test runner itself; Cargo doesn't even interpret it itself.

In the case of --test NAME , NAME is the name of the test source . As in, passing --test blah tells Cargo to build and run the tests in tests/blah.rs . It's the same sort of argument as --bin NAME (for src/bin/NAME.rs ) and --example NAME (for examples/NAME.rs ).

If you really want to only compile a particular subset of tests, the only way I can think of is to use conditional compilation via features. You'd need a package feature for each subset of tests you want to be able to enable/disable.

This functionality has found its way into Cargo. cargo build now has a parameter

 --test [<NAME>]           Build only the specified test target

which builds a binary with the specified set of tests only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM