Hello people, noob question: I have a module inside my crate that only exports one trait.
There is nothing "testable" for this trait but writing an example implementation (and write tests for it).
What's the idiomatic way to do it? Doc tests? Example crate? Example implementation in
Many thanks <3
In general I am also going in order:
- Doc tests
tests in the same file where the tested entity is defined
tests as submodule of module where tested entity is defined, but in separate file
- separate module somewhere in project tree
I am going downside this list when i decide, that there are too many tests, or tests are to long/complex (especially for doctest). This is always fully subjective decision. The (4) I am using actually only, if tests are involving multiple not directly related entities (like system tests).
In your case I would either go for (2) or (3), with example implementation in the same file where I would locate tests.
However consider if your tests are actually tests. If they are really only examples, then I guess that idiomatic way is to create add examples in directory next to your
src, so they would be useable with
cargo run --example foo. Check this blog post: Add examples to your Rust libraries.
If the trait is part of the public API of the crate, only (4), specifically integration test binaries in
tests or in a separate crate in the workspace, will test usage of the trait outside of the crate, so that orphan rules apply as they do for real foreign crates.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.