@@ -23,6 +23,51 @@ To complete
23
23
To complete
24
24
-->
25
25
26
+ ### 4. Guidelines for crate test utilities
27
+
28
+ date: 2025-07-25
29
+ status: Accepted
30
+
31
+ ### Context
32
+
33
+ - For testing, crates may need to define reusable test utilities
34
+ - Some of those utilities may need to be exposed to child crates to enable or facilitate writing tests
35
+ - We want to segregate test utilities from production code as much as possible
36
+ - We want to limit the number of feature flags so the Rust compiler may reuse built artifacts effectively, reducing build time
37
+
38
+ ### Decision
39
+
40
+ Test utilities should be organized following these rules:
41
+
42
+ 1 . Each crate must define its private and public test utilities in a ` test ` module
43
+ 2 . A test utility should be made public if it is reused in a child crate or in the crate's integration tests
44
+ 3 . Making a test utility public should not add any dependency to the crate
45
+ 4 . Private test utilities should be behind ` cfg(test) `
46
+ 5 . Importing a public test utility should always require an import path that contains a ` test ` module, so their misuse
47
+ in production code can be easily identified
48
+ 6 . No feature flag should be added to segregate test utilities from production code
49
+
50
+ Specific cases:
51
+
52
+ - Test doubles (fakes, dummies, mocks, stubs, etc.): should be located in a ` test::double ` module
53
+ - Builders of test data should be located in a ` test::builder ` module
54
+ - Extending a type with a test-only method should be done using extension traits located in the ` test ` module, which
55
+ forces the import of a ` test ` -scoped symbol
56
+ - Their name should be suffixed with ` TestExtension `
57
+ - Implementation of those traits should preferably be below the trait definition, but if some methods need to access
58
+ private properties, then the implementation may be located below their extended type
59
+
60
+ #### Consequences
61
+
62
+ - Enhanced consistency in code organization
63
+ - Improved discoverability of test utilities
64
+ - Clearer distinction between production and test code
65
+ - Simplified maintenance of test utilities
66
+ - Child crates can reuse common test utilities when needed
67
+ - Feature flag usage is minimized, avoiding unnecessary recompilation
68
+
69
+ ---
70
+
26
71
### 3. Guidelines for Dummy Test Doubles
27
72
28
73
** Date:** 2025-07-22
0 commit comments