Rust 如何从'src'导入函数进行测试?

Rust 如何从'src'导入函数进行测试?,rust,Rust,几个小时以来,我一直在试图理解如何导入函数以在Rust中进行测试,但没有成功。我的项目结构如下所示: . ├── Cargo.lock ├── Cargo.toml ├── src │ ├── main.rs │ └── funcs │ ├── mod.rs │ └── hello.rs └── tests └── test_hello.rs src/funcs/mod.rs: pub mod hello; pub fn hello() { println!(&

几个小时以来,我一直在试图理解如何导入函数以在Rust中进行测试,但没有成功。我的项目结构如下所示:

.
├── Cargo.lock
├── Cargo.toml
├── src
│  ├── main.rs
│  └── funcs
│    ├── mod.rs
│    └── hello.rs
└── tests
   └── test_hello.rs
src/funcs/mod.rs

pub mod hello;
pub fn hello() {
    println!("{}", "hello!");
}
mod funcs;

fn main() {
    funcs::hello::hello(); // this works
}
use <crate>::funcs;
src/funcs/hello.rs

pub mod hello;
pub fn hello() {
    println!("{}", "hello!");
}
mod funcs;

fn main() {
    funcs::hello::hello(); // this works
}
use <crate>::funcs;
src/main.rs

pub mod hello;
pub fn hello() {
    println!("{}", "hello!");
}
mod funcs;

fn main() {
    funcs::hello::hello(); // this works
}
use <crate>::funcs;
src/tests/test\u hello.rs

mod funcs; // this import does not work!

#[test]
fn add() {
    assert_eq!(2 + 2, 4);
}

#[test]
fn hello_test() {
    assert_eq!(funcs::hello::hello(), "hello");
}

如何在
src
中导入公共函数,以便它们可以在我的测试目录中使用?

Rust将测试视为单独板条箱的一部分,因此您必须将
使用您的板条箱名称::funcs
在测试顶部,其中
您的_板条箱名称
是在
货物中定义的主包装的板条箱名称。toml

Rust将测试视为单独板条箱的一部分,因此您必须将
使用您的_板条箱名称::funcs
在测试顶部,其中
您的包装箱名称
是您的主包装箱名称,如
货物中所定义。toml

创建一个
src/lib.rs
文件,将包装箱的大部分逻辑放入一个库包装箱中,并在那里导出
funcs
模块:

pub mod funcs;
现在,您可以在任何地方使用库(其中包含模块)。在您的情况下,从
src/main.rs
tests/test\u hello.rs

pub mod hello;
pub fn hello() {
    println!("{}", "hello!");
}
mod funcs;

fn main() {
    funcs::hello::hello(); // this works
}
use <crate>::funcs;
use::funcs;

用与包名和根文件夹相同的库板条箱名称替换

创建
src/lib.rs
文件,将包的大部分逻辑放入库板条箱中,并将
funcs
模块导出到其中:

pub mod funcs;
现在,您可以在任何地方使用库(其中包含模块)。在您的情况下,从
src/main.rs
tests/test\u hello.rs

pub mod hello;
pub fn hello() {
    println!("{}", "hello!");
}
mod funcs;

fn main() {
    funcs::hello::hello(); // this works
}
use <crate>::funcs;
use::funcs;

用与包名和根文件夹相同的库板条箱名称替换

铁锈板条箱可以包含程序和/或库。测试只能访问库,而不能访问程序(并且只能访问库的公共部分)。在您的情况下,您只有一个程序,因此无法进行测试。为了使测试正常工作,您需要:

  • 将代码拆分为程序(在
    main.rs
    文件中)和库(在
    lib.rs
    文件中)
  • 确保要在程序中使用的库的任何部分都是公共的
  • 确保要测试的库的任何部分也是公共的
  • main.rs
    和测试中,编写
    使用foo::hello
    访问
    hello
    函数,用库的名称替换
    foo

如果要将代码拆分为模块,请在
lib.rs
中使用
pub mod_name
声明每个模块,然后使用
use foo::mod_name导入它们
main.rs
或测试中。

生锈板条箱可以包含程序和/或库。测试只能访问库,而不能访问程序(并且只能访问库的公共部分)。在您的情况下,您只有一个程序,因此无法进行测试。为了使测试正常工作,您需要:

  • 将代码拆分为程序(在
    main.rs
    文件中)和库(在
    lib.rs
    文件中)
  • 确保要在程序中使用的库的任何部分都是公共的
  • 确保要测试的库的任何部分也是公共的
  • main.rs
    和测试中,编写
    使用foo::hello
    访问
    hello
    函数,用库的名称替换
    foo

如果要将代码拆分为模块,请在
lib.rs
中使用
pub mod_name
声明每个模块,然后使用
use foo::mod_name导入它们
main.rs
或测试中。

mod
定义模块;它不导入模块。使用
Use
导入模块。应该类似于
使用板条箱::funcs
测试中\u hello.rs
。感谢您的帮助。当我使用
时,使用clarate::funcs
test\u hello.rs
中,我得到错误:
未解析的导入板条箱::funcs根目录中没有funcs
。我在
main.rs
中看到
mod funcs
,因此我不确定该错误的含义main.rs中的code>。
mod
定义了一个模块;它不导入模块。使用
Use
导入模块。应该类似于
使用板条箱::funcs
测试中\u hello.rs
。感谢您的帮助。当我使用
时,使用clarate::funcs
test\u hello.rs
中,我得到错误:
未解析的导入板条箱::funcs根目录中没有funcs
。我在
main.rs
中看到
mod funcs
,因此我不确定该错误的含义
main.rs
中,正如Acorn在下面的评论中所建议的那样。感谢您的帮助。在my Cargo.toml文件中,我的主包名为
hello
。如果我添加
使用hello::funcs
test_hello.rs
文件的顶部,我得到一个错误:
使用未声明的类型或模块hello
;谢谢你的帮助。在my Cargo.toml文件中,我的主包名为
hello
。如果我添加
使用hello::funcs
test_hello.rs
文件的顶部,我得到一个错误:
使用未声明的类型或模块hello
;谢谢你的帮助。当我移动
pub mod funcs时
main.rs
lib.rs
这两个目录都在同一个
src/
目录中,我现在得到一个错误,模块funcs的
文件找不到。Rust进一步声明
帮助:要创建模块
funcs
,请创建文件“src/lib/funcs.rs”
。您使用的是2018版吗?是的,我使用的是2018版。奇怪的是,这应该可以工作。它应该是
pub mod funcs,因为您