[英]How to access functions from the main crate when writing integration tests?
When creating a project with a test like so: 使用如下测试创建项目时:
cargo init --bin projectname
mkdir projectname/tests
echo "extern crate projectname;" > projectname/tests/test.rs
cd projectname/
cargo build
I get this error when testing: 测试时出现此错误:
cargo test
Compiling projectname v0.1.0 (file:///home/username/Lab/projectname)
error[E0463]: can't find crate for `projectname`
--> tests/test.rs:1:1
|
1 | extern crate projectname;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate
How can I access the functions in ´projectname/src/main.rs´ from projectname/tests/test.rs
? 如何从
projectname/tests/test.rs
访问' projectname/tests/test.rs
src / main.rs'中的函数?
How can I access functions in ´projectname/src/main.rs´ from projectname/tests/test.rs?
如何从projectname / tests / test.rs访问'projectname / src / main.rs'中的函数?
You cannot. 你不能。
A binary cannot be used a an external crate (the same way as you can't use a ELF binary as a shared object/library) 二进制文件不能用作外部包(与您不能将ELF二进制文件用作共享对象/库的方式相同)
You just have to change your initialisation to 您只需将初始化更改为
cargo init --lib projectname
or rename your main.rs
to lib.rs
或重命名
main.rs
到lib.rs
If you really want to stick with a main, you may look at Rust package with both a library and a binary? 如果你真的想坚持使用main,你可以看一下带有库和二进制文件的Rust包吗? .
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.