从位于“tests”目录下的集成测试文件中导入“src”目录中定义的函数。

huangapple go评论58阅读模式
英文:

Import function defined in "src" directory from integration test file located under "tests" directory

问题

我在`integration_test.rs`中导入我在`add_two.rs`中定义的公共`add_two`函数时遇到了问题。以下是我的项目结构。

这是我的main.rs文件,定义了主函数。
```rust
// 二进制包:包含主函数

pub mod add_two;

#[cfg(test)]
mod tests {
    use crate::add_two::add_two;

    #[test]
    fn add_two_and_two(){
        assert_eq!(4, add_two(2));
    }
    #[test]
    fn add_three_and_two(){
        assert_eq!(5, add_two(3));
    }
    #[test]
    fn one_hundred(){
        assert_eq!(102, add_two(100));
    }
    #[test]
    #[ignore]
    fn test_we_want_to_ignore() {
        // 需要太多处理时间的代码段!
    }
}

fn main() {

}

这是我的add_two.rs文件。

// 库包

pub fn add_two(a: i32) -> i32 {
    a + 2
}

这是我的integration_test.rs文件。

use crate::add_two::add_two;

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

这是我的cargo.toml文件。

[package]
name = "tutorial1"
version = "0.1.0"
edition = "2021"

[dependencies]

<details>
<summary>英文:</summary>

I am having trouble importing the public `add_two` function I defined in `add_two.rs` over in `integration_test.rs`. Here is my project structure.

```none
Cargo.toml
src/
    main.rs
    add_two.rs
tests/
    integration_test.rs

Here is my main.rs file, which defines main function.

//binary crate: it contains main function

pub mod add_two; 

#[cfg(test)]
mod tests {
    use crate::add_two::add_two; 

    #[test]
    fn add_two_and_two(){
        assert_eq!(4, add_two(2)); 
    }
    #[test]
    fn add_three_and_two(){
        assert_eq!(5, add_two(3)); 
    }
    #[test]
    fn one_hundred(){
        assert_eq!(102, add_two(100)); 
    }
    #[test]
    #[ignore]
    fn test_we_want_to_ignore() {
        //snippet of code that takes too much processing time! 
    }
}

fn main() {

}

Here is my add_two.rs file.

//library crate 

pub fn add_two(a: i32) -&gt; i32 {
    a + 2
}

Here is my integration_test.rs file.

use crate::add_two::add_two; 

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

Here is my cargo.toml file.

[package]
name = &quot;tutorial1&quot;
version = &quot;0.1.0&quot;
edition = &quot;2021&quot;

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

答案1

得分: 2

Integration tests 用于测试一个库。它们的工作方式就像它们是另一个将你的包作为依赖的包。

你的包只有一个二进制 crate,所以集成测试能做的唯一事情就是运行编译后的二进制文件。然而,你可以重构你的代码以使你的测试起作用。

首先,你需要一个 src/lib.rs 文件。这是你的库的入口点。

// lib.rs

pub mod add_two;

然后,在你的集成测试中,通过你的包的名称来引用你的库 crate。

use tutorial1::add_two::add_two; 

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

这将按预期工作,但你还应该清理 main.rs,以便它也依赖于你的库入口点。这可以防止代码被不必要地编译两次。

// main.rs

use tutorial1::add_two;
// 而不是 `mod add_two;`

将你的测试移动到 add_two.rs 也是明智的,因为它们只与该模块相关。这还将确保在测试库时运行它们。

相关链接:

英文:

Integration tests are used to test a library. They work as if they're another package that has your package as a dependency.

Your package only has a binary crate, so the only thing integration tests can do is run the compiled binary. However, you can restructure your code so that your test does work.

First, you need a src/lib.rs file. This is your library's entrypoint.

// lib.rs

pub mod add_two;

Then, in your integration tests, refer to your library crate by the name of your package.

use tutorial1::add_two::add_two; 

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

This will work as intended, but you should also cleanup main.rs so that it depends on your library entrypoint as well. This keeps code from being compiled twice unnecessarily.

// main.rs

use tutorial1::add_two;
// instead of `mod add_two;`

It's also sensible to move your tests into add_two.rs since they are only relevant to that module. This will also ensure they are run when you test the library.

Related:

huangapple
  • 本文由 发表于 2023年6月16日 00:43:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定