Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access functions of a binary from integration tests in Rust's Cargo

In my cargo project I have defined two binary targets. How can I access functions defined in the binary from an integration test?

[package]
name = "passman"
version = "0.1.0"
edition = "2018"

[[bin]]
name = "passmand"
path = "src/daemon/main.rs"

[[bin]]
name = "passman"
path = "src/cli/main.rs"
.
├── Cargo.lock
├── Cargo.toml
├── README.md
├── src
│   ├── cli
│   │   ├── argument
│   │   │   └── mod.rs
│   │   └── main.rs
│   └── daemon
│       ├── entry_value.rs
│       ├── main.rs
│       ├── passman_crypto.rs
│       ├── passman.service
│       └── password_file.rs   <- i want to access functions defined in this
└── tests
    ├── password_file_tests.rs   <- this is my integration test
    └── test.py

I tried to access the file with extern crate passmand;, but Cargo just complains:

 --> tests/password_file_tests.rs:6:1
  |
6 | extern crate passmand;
  | ^^^^^^^^^^^^^^^^^^^^^^ can't find crate
like image 610
jules Avatar asked Dec 10 '25 07:12

jules


1 Answers

In my case, the following codes work around.

#[path = "../src/bin/some_bin.rs"]
mod some_bin;

use some_bin::XXX;

#[test]
fn func_test() {
// ...
}
like image 174
woyo db Avatar answered Dec 13 '25 12:12

woyo db