源代码应该知道它正在被测试吗? [英] Should source code know it's being tested?

查看:24
本文介绍了源代码应该知道它正在被测试吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的代码库,函数之间的耦合紧密,而且我无法轻松编写单元测试.

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.

这篇关于源代码应该知道它正在被测试吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆