- Make file into class and override methods. [...]
- Wrap the whole source file with an #ifdef [...]
If possible, go with 1, as you will have a centralized way of referring to the database code (a class pointer instead of X functions). That means modularity, ease in replacing the implementation (with stubs or with another DB back-end) and more encapsulated code.
If you go with 2, consider replacing the implementation of the functions. That is, inside the original functions, engage the testing code (based on an if
).
Your tested code is completely agnostic if running in a test environment or not, the performance loss is negligible (if(booleanFlagHere)
is a negligible cost in most cases) and you don't have to modify the tested code at all).