简体   繁体   中英

Compile a Rust binary only for use in the crate's integration tests

I'm creating a standalone helper utility for use by some of my integration tests. I could write it in a scripting language like Bash or Python, but it would be convenient if it was a Rust binary since that's what the rest of the project is written in and would avoid complicating the project's testing dependencies. However I'm unsure if there's a good way to create a "test-only" binary. I know I could drop something into bin/ , but it really shouldn't be used outside of tests so that seems like the wrong location (not to mention it would be compiled even when not needed).

Is there any way to define a binary that's only supposed to be compiled for testing? Sort of like the dev-dependencies config but for a binary rather than a dependency. I tried to see if a tests/bin/ directory would work for example, but it didn't appear to get picked up when running cargo test .

I wrote the crate Rust Test Binary to do this exact thing:

If you have integration tests for things that involve subprocess management, inter-process communication, or platform tools, you might need to write some mock binaries of your own to test against. And if you're already using Cargo to build and test, it would be nice to be able to write those test binaries in Rust, near to the crate you're testing, as cargo projects themselves.

This crate provides a simple interface for invoking Cargo to build test binaries organised in a separate directory under your crate.

Note that you might be able to get away with using a binary in examples , which can be simpler. But my crate is for situations where you need different dependencies, features, or generally want some insulation between your "mock" program compilation and your integration tests.

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