简体   繁体   English

在单独的文件中运行 Rust 测试

[英]Running Rust tests inside a separate file

I'm an absolute noob to Rust, and the Rust book doesn't seem to cover my specific use case for unit tests.我是 Rust 的绝对菜鸟,而 Rust 书似乎没有涵盖我的单元测试特定用例。

I'm working on implementing The Computer Language Benchmarks Game for my university degree.我正在为我的大学学位实施计算机语言基准游戏 To be consistent, I have decided that each language and algorithm implementation should abide to the following directory structure:为了保持一致,我决定每种语言和算法的实现都应该遵守以下目录结构:

root
|
|__ language_1
|   |
|   |__ algorithm_1
|   |   |
|   |   |__ algorithm_1          <-- The algorithm logic.
|   |   |
|   |   |__ algorithm_1_tests    <-- Tests.
|   |   |
|   |   |__ algorithm_1_run      <-- A `main` function to execute the algorithm logic.
|   |
|   |__ algorithm_2
|
|__ language_2
...

I require this consistency because I will later write a bash script to traverse this directory tree structure and be able to easily compile (if needed) and run the necessary files.我需要这种一致性,因为我稍后会编写一个 bash 脚本来遍历这个目录树结构,并且能够轻松编译(如果需要)和运行必要的文件。

I'm currently learning and writing an algorithm in the Rust programming language.我目前正在学习和编写 Rust 编程语言的算法。 I created the algorithm as a library file and have a separate file that calls the library file and executes the function inside.我将算法创建为库文件,并有一个单独的文件调用库文件并在内部执行 function。 This works fine.这工作正常。

My problem lies with testing.我的问题在于测试。 I wish to have a separate file containing all the tests, imports the original algorithm logic file, and calls (and asserts) the functions.我希望有一个包含所有测试的单独文件,导入原始算法逻辑文件,并调用(和断言)函数。

To make this clearer, I'll provide a mock example of what it is I'm running:为了使这一点更清楚,我将提供一个我正在运行的模拟示例:

root
|
|__ rust
|   |
|   |__ algorithm
|   |   |
|   |   |__ algorithm.rs
|   |   |
|   |   |__ algorithm_tests.rs
|   |   |
|   |   |__ algorithm_run.rs

algorithm.rs

fn is_even(a: u32) -> bool {
    return a % 2 == 0;
}

algorithm_run.rs

mod algorithm;

fn main() {
    let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    for number in numbers {
        algorithm::is_even(number);
    }

algorithm_tests.rs

mod algorithm;

#[test]
fn test_is_even_returns_true_for_even_numbers() {
    let even_numbers = [2, 4, 6, 8, 10]
    for number in even_numbers {
        assert_eq!(true, algorithm::is_even(number));
    }
}

This is executed and run with the following command $ rustc algorithm_run.rs -o algorithm &&./algorithm这是使用以下命令执行和运行的$ rustc algorithm_run.rs -o algorithm &&./algorithm

What command or modifications will I need to make to run the tests?我需要进行哪些命令或修改才能运行测试?

You can pass --test to compile with the test harness:您可以通过--test与测试工具一起编译:

$ rustc --test algorithm_tests.rs && ./algorithm_tests

running 1 test
test test_is_even_returns_true_for_even_numbers ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

fwiw The benchmarks game project website provides makefile scripts to build the different test programs & make measurements of the programs. fwiw基准游戏项目网站提供 makefile 脚本来构建不同的测试程序并对程序进行测量。

https://salsa.debian.org/benchmarksgame-team/benchmarksgame/-/tree/master/bencher https://salsa.debian.org/benchmarksgame-team/benchmarksgame/-/tree/master/bencher

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

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