你把单元测试放在哪里? [英] Where do you put your unit test?

查看:32
本文介绍了你把单元测试放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中发现了几个内务单元测试的约定,并且我不确定哪种方法适合我们的下一个 PHP 项目.我是试图找到最好的约定来鼓励简单的开发和查看源代码时测试的可访问性.我会很对您的经验/意见感兴趣:

I have found several conventions to housekeeping unit tests in a project and I'm not sure which approach would be suitable for our next PHP project. I am trying to find the best convention to encourage easy development and accessibility of the tests when reviewing the source code. I would be very interested in your experience/opinion regarding each:

  1. 一个文件夹用于生产代码,另一个用于单元测试:这将分开来自项目逻辑文件的单元测试.这种分离顾虑既是麻烦又是优势:有人正在调查该项目的源代码将 - 所以我想 - 要么浏览实现或单元测试(或更常见的是:实现只要).单元测试的优势是您的类的另一个观点丢失了 - 这两个观点与 IMO 相差太远.
  2. 带注释的测试方法:我知道的任何现代单元测试框架都允许开发人员创建专用的测试方法,对它们进行注释 (@test) 和将它们嵌入到项目代码中.我在这里看到的最大缺点是项目文件变得混乱.即使将这些方法分开使用注释标题(如此行下方的单元测试)它只会膨胀不必要地上课.
  3. 与实现文件位于同一文件夹中的测试文件:我们的文件命名约定规定 PHP 文件包含类(一个类每个文件)应该以 .class.php 结尾.我可以想象推杆单元将一个类文件测试到另一个以 .test.php 结尾的文件中将测试更多地呈现给其他开发人员而不会污染班上.虽然它使项目文件夹膨胀,而不是实现文件,这是我迄今为止最喜欢的,但我有我的疑虑:我会认为其他人已经提出了这个,并丢弃了这个出于某种原因的选项(即我还没有看到带有文件的 java 项目Foo.javaFooTest.java 在同一文件夹中.)也许是因为Java 开发人员更多地使用 IDE,这让他们更容易访问测试,而在 PHP 中没有出现大型编辑器(例如 eclipse forjava) - 我认识的许多开发人员都使用 vim/emacs 或类似的编辑器支持 PHP 开发本身.
  1. One folder for productive code, another for unit tests: This separates unit tests from the logic files of the project. This separation of concerns is as much a nuisance as it is an advantage: Someone looking into the source code of the project will - so I suppose - either browse the implementation or the unit tests (or more commonly: the implementation only). The advantage of unit tests being another viewpoint to your classes is lost - those two viewpoints are just too far apart IMO.
  2. Annotated test methods: Any modern unit testing framework I know allows developers to create dedicated test methods, annotating them (@test) and embedding them in the project code. The big drawback I see here is that the project files get cluttered. Even if these methods are separated using a comment header (like UNIT TESTS below this line) it just bloats the class unnecessarily.
  3. Test files within the same folders as the implementation files: Our file naming convention dictates that PHP files containing classes (one class per file) should end with .class.php. I could imagine that putting unit tests regarding a class file into another one ending on .test.php would render the tests much more present to other developers without tainting the class. Although it bloats the project folders, instead of the implementation files, this is my favorite so far, but I have my doubts: I would think others have come up with this already, and discarded this option for some reason (i.e. I have not seen a java project with the files Foo.java and FooTest.java within the same folder.) Maybe it's because java developers make heavier use of IDEs that allow them easier access to the tests, whereas in PHP no big editors have emerged (like eclipse for java) - many devs I know use vim/emacs or similar editors with little support for PHP development per se.

您对这些单元测试展示位置的体验如何?你有我没有在这里列出的另一个约定?或者我只是高估了单元测试审阅者的可访问性?

What is your experience with any of these unit test placements? Do you have another convention I haven't listed here? Or am I just overrating unit test accessibility to reviewers?

推荐答案

我喜欢将单元测试保存在与生产代码 (#3) 相同的目录中的单独源文件中.

I favour keeping unit-tests in separate source files in the same directory as production code (#3).

单元测试不是二等公民,它们的代码必须像生产代码一样维护和重构.如果您将单元测试保存在单独的目录中,那么下一个更改您的生产代码的开发人员可能会错过它的单元测试并且无法维护这些测试.

Unit tests are not second-class citizens, their code must maintained and refactored just like production code. If you keep your unit tests in a separate directory, the next developer to change your production code may miss that there are unit tests for it and fail to maintain the tests.

在 C++ 中,我倾向于每个类有三个文件:

In C++, I tend to have three files per class:

MyClass.h
MyClass.cpp
t_MyClass.cpp

如果您使用 Vim,那么我的 toggle_unit_tests 插件-in 用于在源和单元测试文件之间切换可能会很有用.

If you're using Vim, then my toggle_unit_tests plug-in for toggling between source and unit test files may prove useful.

这篇关于你把单元测试放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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