I have a crate library with an optional deserialize feature. I want to write tests for deserialization using some crate like serde_json, but I don't need this dependency publicly. So my Cargo.toml looks like this:
Looking at discussion in Activating features for tests/benchmarks · Issue #2911 · rust-lang/cargo · GitHub, there seems to be no dedicated way to do this. The issue thread contains some discussion of various potential workarounds. The most straightforward approach is to just run the tests using cargo test --features deserialize.
For workflows with different combinations of features, one may also use cargo-hack: for command in clippy test ; do cargo hack --feature-powerset $command ; done