Java 中重载的单元测试方法的命名

发布于 2024-11-19 09:27:38 字数 208 浏览 1 评论 0原文

当目标重载时,最可接受的命名单元测试方法的方法是什么。考虑这些方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假扮的天使 2024-11-26 09:27:38

尽一切努力让您和您的同事(如果有的话)更容易阅读。我认为这取决于您对该类的其他测试是什么,但基于这两种方法,我要做的是:

测试 doSomething():

  • doSomething_void_success 的测试方法(这将是测试成功路径的一些测试)
  • doSomething_void_fail (这将是一些测试不正确路径的测试)
  • doSomething_void_someOtherTest

测试 doSomething(String) 的测试方法:

  • doSomething_String_success
  • doSomething_String_fail
  • doSomething_String_someOtherTest

我不再使用 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():

  • doSomething_void_success (this would be some test that tests a successful path)
  • doSomething_void_fail (this would be some test that tests an incorrect path)
  • doSomething_void_someOtherTest

Test methods that test doSomething(String):

  • doSomething_String_success
  • doSomething_String_fail
  • doSomething_String_someOtherTest

I don't use the test prefix anymore, as JUnit 4 doesn't require it. I just use the @Test annotation

等你爱我 2024-11-26 09:27:38

没有单一的“最被接受的方式”——选择你(团队)认为最可读和最干净的方式。

我个人不再使用 test 前缀,因为从 JUnit 4 开始就没有必要了,而且它会降低可读性和可搜索性。我尝试根据测试的场景来命名我的测试方法。在你的简单情况下可能是

doSomethingSuccessfully();
...
failsToDoSomethingWithAString();
...
doSomethingWithAStringAndFail();

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

doSomethingSuccessfully();
...
failsToDoSomethingWithAString();
...
doSomethingWithAStringAndFail();
心清如水 2024-11-26 09:27:38

我认为这是您的开发环境约定的问题。

我更喜欢使用下划线,因为它可以更清晰地表示测试中的方法。

我仍然使用 test 前缀,尽管其他人指出这不是必需的。我通常这样做是为了将实际测试与测试类中可能存在的一些辅助方法分开。

所以,对于你的情况,我会这样做

test_doSomething
test_doSomething_String

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

test_doSomething
test_doSomething_String
深海夜未眠 2024-11-26 09:27:38

使用所需的行为来命名您的测试方法,例如:

/**
*
* @return
* @should say hello, and nothing more that that
*/
String sayHello();
}

将创建一个像这样的测试方法:

@Test
public void sayHello_shouldSayHelloAndNothingMoreThatThat() throws Exception {
//TODO auto-generated
Assert.fail("Not yet implemented");
}

我为 IntelliJ IDEA 开发了一个插件(Eclipse 版本也存在)来为您创建这些测试方法,您可以在这里找到它:

http://plugins.intellij.net/plugin/?idea&id=5847

Use the desired behaviours to name your test methods, for example:

/**
*
* @return
* @should say hello, and nothing more that that
*/
String sayHello();
}

Would create a test method like this one:

@Test
public void sayHello_shouldSayHelloAndNothingMoreThatThat() throws Exception {
//TODO auto-generated
Assert.fail("Not yet implemented");
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文