测试代码是否应该与源代码/生产代码分开 [英] Should Test Code be Separate from Source/Production Code

查看:34
本文介绍了测试代码是否应该与源代码/生产代码分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使我们有一个 Makefile 或类似的东西来在运送产品时分隔测试代码.在我看来,它们应该是分开的,但我不完全相信为什么

Even if we have a Makefile or something similar to separate the test code when shipping product. In my opinion, they should be separate, but i am not entirely convinced as to WHY

推荐答案

是的,它们应该是分开的(文件夹和项目最好).一些原因:

Yes, they should be separate (folders and preferably projects). Some reasons:

  • GREP.在生产源中搜索字符串更容易.
  • 代码覆盖率.想象一下,尝试指定要包含哪些文件以进行覆盖.
  • 不同的标准.您可能只想对生产代码运行静态分析等.
  • 简化的生成文件/构建脚本.

现代 IDE 将允许您处理来自不同项目/文件夹的代码,就像它们是相邻的一样.

Modern IDEs will allow you to work on code from separate projects/folders as if they were adjacent.

最糟糕的事情是将测试和生产代码包含在同一个文件中(使用条件编译、不同的入口点等).这不仅会使试图阅读代码的开发人员感到困惑,而且您总是冒着意外传送测试代码的风险.

The worst thing you can do is to include test and production code in the same file (with conditional compilation, different entry points, etc.). Not only can this confuse developers trying to read the code, you always run the risk of accidentally shipping test code.

这篇关于测试代码是否应该与源代码/生产代码分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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