Enabling a function for unit tests only

I have 2 crates (my code for both): common & thing. Inside of my common crate, I have an immutable struct (pass it all values on creation, can't change them later) Foo. However, while unit testing my thing crate, it'd be super helpful to change one of the values in Foo. I tried the following:

impl Foo {
pub fn change_value(&mut self, new_value: u64) { self.value = new_value; }

However, when I run the unit tests for thing I get a "method not found" error. I'm assuming this is because the test config is not enabled while compiling common. I tried adding my common crate to both [dependencies] and [dev-dependencies], but that didn't do it either.

Is there any way to get my common create to build with cfg(test) so that function is compiled and I can call it, but from non-test code it is as if that function doesn't actually exist?

I've seen this question asked before and my understanding is that there's no good way to do this. Even if you added a new feature to common, setting it in the dev-dependencies of thing would enable it for all other builds as well (see https://github.com/rust-lang/cargo/issues/7916 for further info and work on a flag to prevent this from happening).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.