Why are doctests exempted from conditional compilation for tests?
⚓ Rust 📅 2025-11-10 👤 surdeus 👁️ 5Why are doctests exempted from conditional compilation for tests? 
Let's look at the following code. It defines a single constant ARGON2_MEMORY_COST using conditional compilation: If we're testing, then set it small, otherwise set it large. Below that constant, we have both a unit test, as well as a doctest. Both types of tests have assert_eq!(ARGON2_MEMORY_COST, 1024). However, only the unit test passes. The doctest has somehow been compiled under the non-test code, as we will later see.
#![allow(dead_code)]
#[cfg(test)]
pub const ARGON2_MEMORY_COST: u32 = 1024; // smaller for faster tests
#[cfg(not(test))]
pub const ARGON2_MEMORY_COST: u32 = 65536; // larger for enhanced security
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn memory_cost_is_small() {
assert_eq!(ARGON2_MEMORY_COST, 1024);
}
}
/// Placeholder struct just so we can show an example of a doctest.
///
/// # Examples
/// ```rust
/// use doctests_conditional_compilation::ARGON2_MEMORY_COST;
///
/// assert_eq!(ARGON2_MEMORY_COST, 1024);
/// ```
struct UnitStruct;
This is the output for the unit test:
running 1 test
test tests::memory_cost_is_small ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
On the other hand, this is the output for the doctest:
Doc-tests doctests_conditional_compilation
running 1 test
test src/lib.rs - UnitStruct (line 20) ... FAILED
failures:
---- src/lib.rs - UnitStruct (line 20) stdout ----
Test executable failed (exit status: 101).
stderr:
thread 'main' panicked at /tmp/rustdoctestCdCfPy/doctest_bundle_2024.rs:8:1:
assertion `left == right` failed
left: 65536
right: 1024
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
src/lib.rs - UnitStruct (line 20)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
So, why are doctests excluded like this from conditional compilation which is explicitly aimed at test cases? And how can I configure the conditional compilation to include doctests in my testing code?
Any insight would be appreciated. ![]()
2 posts - 2 participants
🏷️ Rust_feed