问题
I have a complex codebase with tight couplings between functions and I am not able to write unit tests easily.
Should source code know about testing environment, should it know it's being tested?
To indicate it's being tested or so can be easily via global flag but I have a fear it may cause a bigger mess in the long run.
回答1:
In short, no.
Your code should be written in such a fashion that it is testing-agnostic. What I mean is that it shouldn't care if it is being tested or not. Because of your 'tight couplings' I would suggest that you do your testing as manually as you can since that would give you the best litmus test of it working as expected.
Also, if your code is implemented well enough it would also be environment agnostic. Whatever environment you test in should be as close to real-world as possible.
来源:https://stackoverflow.com/questions/17348260/should-source-code-know-its-being-tested