Java 中重载的单元测试方法的命名
当目标重载时,最可接受的命名单元测试方法的方法是什么。考虑这些方法:
doSomething();
doSomething(String);
您将如何命名相应的测试方法?这是最容易接受的方式吗?
testDoSomething();
testDoSomethingString();
What is the most accepted way to name a unit test method when the target has overloads. Consider these methods:
doSomething();
doSomething(String);
How would you name the corresponding test methods? Would this be the most accepted way?
testDoSomething();
testDoSomethingString();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽一切努力让您和您的同事(如果有的话)更容易阅读。我认为这取决于您对该类的其他测试是什么,但基于这两种方法,我要做的是:
测试 doSomething():
测试 doSomething(String) 的测试方法:
我不再使用 test 前缀,因为 JUnit 4 不需要它。我只是使用@Test注释
Do whatever makes things more readable for you and your co-workers, if any. I think it depends on what your other tests for that class are, but based on those two methods, here is what I would do:
Test methods that test doSomething():
Test methods that test doSomething(String):
I don't use the test prefix anymore, as JUnit 4 doesn't require it. I just use the @Test annotation
没有单一的“最被接受的方式”——选择你(团队)认为最可读和最干净的方式。
我个人不再使用
test
前缀,因为从 JUnit 4 开始就没有必要了,而且它会降低可读性和可搜索性。我尝试根据测试的场景来命名我的测试方法。在你的简单情况下可能是There is no single "most accepted way" - pick what you(r team) feel is most readable and clean.
I personally don't use the
test
prefix anymore, as it is not necessary since JUnit 4, and it degrades readability and searchability. I try to name my test methods after the scenarios they test. Which in your simplistic case could possibly be我认为这是您的开发环境约定的问题。
我更喜欢使用下划线,因为它可以更清晰地表示测试中的方法。
我仍然使用
test
前缀,尽管其他人指出这不是必需的。我通常这样做是为了将实际测试与测试类中可能存在的一些辅助方法分开。所以,对于你的情况,我会这样做
I think it's the matter of your dev environment conventions.
I prefer to use underscores, as it allows cleaner representations of methods under test.
I also, still use
test
prefix, though others pointed out that it's not required. I usually do this in order to separate actual tests from some helper methods that may be in the test class.So, in your case, I would do
使用所需的行为来命名您的测试方法,例如:
将创建一个像这样的测试方法:
我为 IntelliJ IDEA 开发了一个插件(Eclipse 版本也存在)来为您创建这些测试方法,您可以在这里找到它:
http://plugins.intellij.net/plugin/?idea&id=5847
Use the desired behaviours to name your test methods, for example:
Would create a test method like this one:
I have developed a plugin for IntelliJ IDEA (Eclipse version exists too) for creating these test methods for you, you can find it here:
http://plugins.intellij.net/plugin/?idea&id=5847