Unresolved Import & Import Resolution Stuck

I'm using the speculate package for writing tests in an rspec-like nature, cargo test compiles and executes without issue, but I get this error otherwise when running cargo run:

unresolved import `speculate`
use of undeclared type or module `speculate

I have speculate listed as a dev dependency in my Cargo.toml file. Rust Analyzer also picks it up as a syntax error. I've tried moving my tests to a completely separate file under a separate /tests sub-directory which isn't included by any of the project's implementing files, yet the tests don't run in this case. My guess is because they're not included within the project's implementing files.

And that means it's available only for tests/benches, not for the library itself. Move it to the ordinary dependencies, and it should work. Or, if you need it only for tests, put #cfg[test] on every use from it.

Ah, I never thought to use #[cfg(test)] on an imported module before. That said, I imported my module at the top of my crate, and added that macro declaration to it. The tests work fine, and the build runs too.

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.