【发布时间】:2022-01-15 05:57:53
【问题描述】:
我有一些通用功能,我想在不同包中的 junit 测试中重用这些功能,但我不知道该怎么做。我尝试过使用不同的模块和库,但有些东西不起作用。我目前的结构看起来像这样:
.
├── Cargo.lock
├── Cargo.toml
├── foo
│ └── bar
│ └── baz
│ ├── template
│ │ ├── Cargo.lock
│ │ ├── Cargo.toml
│ │ └── src
│ │ └── tests
│ │ ├── mod.rs
│ │ └── test1.rs
│ │
│ └── template_implementation
│ ├── Cargo.lock
│ ├── Cargo.toml
│ └── src
│ └── tests
│ ├── mod.rs
│ └── test2.rs
│
└── utils
└── testpackage
├── Cargo.toml
├── lib.rs
└── common.rs
所以在common.rs 我有这样的东西:
/// setup for tests
pub fn setup() { ... }
pub fn mock_x() { ... }
pub fn mock_y() { ... }
lib.rs 看起来像这样:
#[cfg(test)]
pub mod common;
我想在test1.rs 和test2.rs 中使用common.rs 中的这些函数。我确保货物文件的依赖项是正确的,即使它们被 IDE “找到”,我在执行 cargo test 时收到错误,即在 testpackage 中找不到这些方法。
如果我从 mod 中删除 #[cfg(test)] 它“有效”,但我不希望在执行 cargo build 时包含它,此外我在运行测试时收到一堆警告,它甚至不编译然后是工件。
我尝试移动 common.rs,例如将其包含在 template 包中,但仍然无法正常工作,我面临类似的问题。不确定我是否遗漏了一些注释或什么,如果感觉像一个非常愚蠢的问题。
你会怎么做这样的事情?
【问题讨论】:
-
这个问题与 JUnit 有什么关系?
-
对,错标签
-
这看起来像是一个非常规的 Rust 项目文件夹结构......你是在使用 Cargo 工作空间还是其他东西来管理它?
-
我确实在使用 cargo,我有多个 crate 来分离关注点,这有什么不寻常的地方? @麦粒肿
-
我发现嵌套级别非常规(
foo/bar/baz/层在两个文件夹上方具有实际 crate 定义),但是你可能只是省略了不相关的代码库部分
标签: unit-testing testing rust rust-cargo