如何将测试移动到 Rust 货物中二进制文件的单独文件中? [英] How to move tests into a separate file for binaries in Rust's Cargo?

查看:84
本文介绍了如何将测试移动到 Rust 货物中二进制文件的单独文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Cargo 创建了一个新的二进制文件:

I created a new binary using Cargo:

cargo new my_binary --bin

my_binary/src/main.rs 中的一个函数可用于测试:

A function in my_binary/src/main.rs can be used for a test:

fn function_from_main() {
    println!("Test OK");
}

#[test]
fn my_test() {
    function_from_main();
}

并且 cargo test -- --nocapture 按预期运行测试.

And cargo test -- --nocapture runs the test as expected.

将此测试移动到单独文件中的最直接方法是什么(将 function_from_main 保留在 my_binary/src/main.rs 中)?

What's the most straightforward way to move this test into a separate file, (keeping function_from_main in my_binary/src/main.rs)?

我尝试这样做,但不知道如何让 my_test 从单独的文件中调用 function_from_main.

I tried to do this but am not sure how to make my_test call function_from_main from a separate file.

推荐答案

The Rust Programming Language一章专门用于测试,您应该阅读它以获得基本的理解.

The Rust Programming Language has a chapter dedicated to testing which you should read to gain a baseline understanding.

通常将单元测试(允许访问代码内部的测试)放入每个特定文件的 test 模块中:

It's common to put unit tests (tests that are more allowed to access internals of your code) into a test module in each specific file:

fn function_from_main() {
    println!("Test OK");
}

#[cfg(test)]
mod test {
    use super::*;
    
    #[test]
    fn my_test() {
        function_from_main();
    }
}

模块可以移动到新文件,尽管这对于单元测试模块来说并不常见:

Modules can be moved to new files, although this is uncommon for the unit test module:

ma​​in.rs

fn function_from_main() {
    println!("Test OK");
}

#[cfg(test)]
mod test;

test.rs

use super::*;

#[test]
fn my_test() {
    function_from_main();
}

参见 将模块分成不同的文件,了解有关文件和模块如何相互映射的详细信息.

See Separating Modules into Different Files for detailed information on how files and modules map to each other.

单独文件中的测试更常见的情况是集成测试.书中的部分也涵盖了这些内容专门用于板条箱外的测试.这些类型的测试非常适合作为代码使用者来练习代码.

The more common case for tests in a separate file are integration tests. These are also covered in the book by a section devoted to tests outside of the crate. These types of tests are well-suited for exercising the code as a consumer of your code would.

文档的该部分包括介绍性示例和描述性文本:

That section of the documentation includes an introductory example and descriptive text:

我们在项目目录的顶层创建一个 tests 目录,src 旁边.Cargo 知道在此查找集成测试文件目录.然后我们可以在这个中制作任意数量的测试文件目录,Cargo 会将每个文件编译为单独的板条箱.

We create a tests directory at the top level of our project directory, next to src. Cargo knows to look for integration test files in this directory. We can then make as many test files as we want to in this directory, and Cargo will compile each of the files as an individual crate.

让我们创建一个集成测试.仍然使用清单 11-12 中的代码在 src/lib.rs 文件中,创建一个 tests 目录,创建一个新文件命名为 tests/integration_test.rs,并输入示例 11-13 中的代码:

Let’s create an integration test. With the code in Listing 11-12 still in the src/lib.rs file, make a tests directory, create a new file named tests/integration_test.rs, and enter the code in Listing 11-13:

文件名:tests/integration_test.rs

Filename: tests/integration_test.rs

use adder;

#[test]
fn it_adds_two() {
    assert_eq!(4, adder::add_two(2));
}

示例 11-13:加法器箱中函数的集成测试

我们在代码顶部添加了use adder,我们不需要单元测试.原因是tests目录下的每个测试都是一个单独的板条箱,所以我们需要将我们的库带入每个测试crate 的范围.

We’ve added use adder at the top of the code, which we didn’t need in the unit tests. The reason is that each test in the tests directory is a separate crate, so we need to bring our library into each test crate’s scope.

请注意,该函数被称为adder::add_two.关于 Rust 模块系统的更多细节可以在 包、板条箱和模块章节.

Note that the function is called as adder::add_two. Further details about Rust's module system can be found in the Packages, Crates, and Modules chapter.

由于这些测试会像用户一样运行您的 crate,如果您想测试二进制,您应该执行二进制.像 assert_cmd 这样的 crate 可以帮助减轻此类测试的痛苦.

Since these tests exercise your crate as a user would, if you want to test a binary, you should be executing the binary. Crates like assert_cmd can help reduce the pain of this type of test.

在其他情况下,您应该将大二进制文件分解为一个大库和一个小二进制文件.然后,您可以为库的公共 API 编写集成测试.

In other cases, you should break your large binary into a large library and a small binary. You can then write integration tests for the public API of your library.

另见:

这篇关于如何将测试移动到 Rust 货物中二进制文件的单独文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆