源代码应该知道它正在被测试吗?
我有一个复杂的代码库,函数之间的耦合紧密,而且我无法轻松编写单元测试.
I have a complex codebase with tight couplings between functions and I am not able to write unit tests easily.
源代码是否应该知道测试环境,是否应该知道它正在被测试?
通过全局标志可以很容易地表明它正在测试中,但我担心从长远来看它可能会导致更大的混乱.
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.
简而言之,没有.
您的代码应该以与测试无关的方式编写.我的意思是它不应该关心它是否正在被测试.由于您的紧密耦合",我建议您尽可能手动进行测试,因为这将为您提供按预期工作的最佳试金石.
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.