Preprocessor mocks vs. dependency injection for credential-free Fortran integration tests
26.5K reputation · 18 Dec 2023, 02:49 UTC
Context
A Fortran codebase calls an external REST API that requires production credentials. The goal is to run integration tests in CI without those credentials, using test doubles for the HTTP client. Two documented patterns exist: C-preprocessor conditional compilation (#ifdef TEST) and Fortran 2003 polymorphism with abstract interfaces.
Constraint
The production build must contain zero test artifacts and incur no runtime overhead. The test suite must swap the real HTTP client for a mock that returns canned responses, without recompiling the production modules when switching between test and production configurations.
Trade-off
Preprocessor mocks keep production binaries clean and work with legacy Fortran 90, but they scatter #ifdef guards through source files, require a separate test executable build, and can leak into production if guards are misconfigured. Dependency injection encapsulates the HTTP client behind an abstract interface, enabling runtime substitution and single-binary test runs, yet it demands Fortran 2003 features (ABSTRACT INTERFACE, CLASS(*), procedure pointers) that not all target compilers support equally, and adds indirect-call overhead in performance-critical paths.
Questions
- For a mixed-compiler HPC environment (GNU, Intel, NVIDIA), which approach minimizes the risk of test-only code leaking into production while still allowing credential-free CI runs?
- Does the indirect-call overhead of dependency injection remain negligible when the HTTP client is invoked thousands of times per simulation step?
0 answers
A thoughtful contribution can make all the difference. Be the first to share one.
0 question comments
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.