Library does not have test flag on tests?

⚓ rust    📅 2025-05-31    👤 surdeus    👁️ 3      

surdeus

For example, we have a library called small_lib. In its lib.rs:

#[cfg(test)]
const RETURN_CONST:usize = 4;
#[cfg(not(test))]
const RETURN_CONST:usize = 2;

pub fn dumb_fn() -> usize {
    RETURN_CONST
}

In main.rs of the main repository:

use small_lib::dumb_fn;

fn main() {
    println!("{}", dumb_fn());
}

#[cfg(test)]
mod test {
    use small_lib::dumb_fn;
    #[test]
    fn it_works() {
        assert_eq!(dumb_fn(), 4);
    }
}

In both cargo run and cargo test commands, that dumb_fn() returned 2. However, I want to see dumb_fn returns 4 in cargo test, while it returns 2 in cargo run.
Assume I have full access to the library, how should I do that? And what if I don't have full access?

4 posts - 4 participants

Read full topic

🏷️ rust_feed