简体   繁体   English

编译一个 Rust 二进制文件,仅用于 crate 的集成测试

[英]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.我可以用脚本语言编写它,如 Bash 或 Python,但如果它是 Rust 二进制文件会很方便,因为项目的 rest 就是这样编写的,并且会避免使项目的测试依赖项复杂化。 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).我知道我可以将一些东西放到bin/中,但它真的不应该在测试之外使用,所以它看起来像是错误的位置(更不用说即使不需要它也会被编译)。

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.有点像dev-dependencies配置但是是二进制而不是依赖。 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 .例如,我试图查看tests/bin/目录是否可以工作,但在运行cargo test时似乎没有被拾取。

I wrote the crate Rust Test Binary to do this exact thing:我写了 crate Rust Test Binary来做这件事:

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.如果您已经在使用 Cargo 来构建和测试,那么能够在 Rust 中编写这些测试二进制文件会很好,靠近您正在测试的板条箱,作为货物项目本身。

This crate provides a simple interface for invoking Cargo to build test binaries organised in a separate directory under your crate.这个 crate 提供了一个简单的接口,用于调用 Cargo 来构建在你的 crate 下的单独目录中组织的测试二进制文件。

Note that you might be able to get away with using a binary in examples , which can be simpler.请注意,您可以在examples中使用二进制文件,这样会更简单。 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.但是我的 crate 适用于需要不同的依赖项、特性,或者通常希望在“模拟”程序编译和集成测试之间进行一些隔离的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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