Unit testing 测试定制板条箱

Unit testing 测试定制板条箱,unit-testing,rust,rust-crates,Unit Testing,Rust,Rust Crates,我在/src/lib.rs中找到了这个板条箱,我正在尝试对其运行测试: #![crate_type = "lib"] #![crate_name = "mycrate"] pub mod mycrate { pub struct Struct { field: i32, } impl Struct { pub fn new(n: i32) -> Struct { Struct { field: n }

我在
/src/lib.rs
中找到了这个板条箱,我正在尝试对其运行测试:

#![crate_type = "lib"]
#![crate_name = "mycrate"]

pub mod mycrate {
    pub struct Struct {
        field: i32,
    }

    impl Struct {
        pub fn new(n: i32) -> Struct {
            Struct { field: n }
        }
    }
}
测试文件位于
/tests/test.rs

extern crate mycrate;

use mycrate::*;

#[test]
fn test() {
    ...
}
运行
cargo test
会出现以下错误:

tests/test.rs:3:5: 3:16 error: import `mycrate` conflicts with imported crate in this module (maybe you meant `use mycrate::*`?) [E0254]
tests/test.rs:3 use mycrate::*;
                     ^~~~~~~~~

我在这里做错了什么?

板条箱也会自动成为自己名字的模块。因此,不需要指定子模块。由于导入了
mycratet
cratet中的所有内容,因此还导入了
mycratet::mycratet
模块,这导致了命名冲突

只需将
src/lib.rs
的内容更改为

pub struct Struct {
    field: i32,
}

impl Struct {
    pub fn new(n: i32) -> Struct {
        Struct { field: n }
    }
}
也不需要
板条箱名称
板条箱类型
属性