Rust中如何针对特定模块关闭cargo doc test并排除生成的.rs文件
Primary Working Solution
Generated gRPC output files are pulled into your crate via hand-written mod declarations or include! macros that you control. You can disable doc tests for the entire generated code tree without editing any generated files by using the #[doc(test(ignore))] attribute on the root of your generated code module.
For generated code organized under a dedicated submodule (e.g. the
src/googletree from your use case):
Modify only themoddeclaration in your hand-written crate root (likesrc/lib.rs):#[doc(test(ignore))] pub mod google;This attribute applies to all nested modules under
google, including every generated .rs file in its subdirectories. All doc tests (and inline comment code blocks from generated code) will be skipped automatically when runningcargo test.For generated files pulled in via
include!macros:
Wrap theinclude!call in a hand-written module with the same attribute:#[doc(test(ignore))] mod generated_bigtable_v2 { include!("./google/google.bigtable.v2.rs"); }
Optional Secondary Approach
If your crate consists only of generated code (no hand-written business logic), you can disable all doc tests for the entire crate by adding this line to your crate root (src/lib.rs or src/main.rs):
#![doc(test(ignore))]
This is only suitable for dedicated generated code crates, as it will skip all doc tests for your hand-written code too if added to a mixed crate.
Practices to Avoid
- Manually editing generated .rs files: Changes will be overwritten when you regenerate code from proto files.
- Relying on manual
cargo testflags (like--skip): These add friction for CI/CD and other developers running the test suite.
内容的提问来源于stack exchange,提问作者Fuyang Liu

