单位中测试方法的前缀:“测试”与“应该”

发布于 2024-10-08 19:25:39 字数 597 浏览 3 评论 0 原文

在 JUnit 中的测试方法名称中添加“test”前缀是一种常见的做法。但最近几年,有些人将其改为前缀“应该”。

如果我想在数据库中测试客户创建,我通常会将方法命名为“testCustomerCreation”。然而,有些人会命名为“shouldCreateCustomer”。

当我是项目中唯一的人或者项目中的其他人都同意我的观点时,这是很多个人品味的体现。但当情况并非如此时,一些分歧或不一致的混合就会开始出现。

我在某处读到一个人的文章,将他的方法命名为“testShouldCreateCustomer”,因此他决定删除“test”前缀。但事实上他并没有使用“test”作为前缀,而是使用了“testShould”并改为“should”。显然,这并没有说服我。

我个人强烈倾向于坚持使用“test”前缀,因为方法名称通常以不定式形式的动词开头(“get”、“set”、“add”、“remove”、“clear”、“send”、“接收”、“打开”、“关闭”、“读取”、“写入”、“创建”、“列表”、“弹出”、“打印”等,“测试”也是如此)。因此,在方法名称前加上“应该”对我来说听起来真的很奇怪,看起来是错误的。

那么,使用“应该”而不是“测试”的真正充分理由是什么?有哪些巨大的优点和缺点?

It is a common practice to prefix the tests method names in JUnit with "test". But in the last few years, some people changed this to the prefix "should".

If I want to test the customer creation in a database, I would normally name the method "testCustomerCreation". However, some people would name "shouldCreateCustomer".

This is a lot of personal taste when I am the only person in the project or when everyone else in the project agrees with me. But when/where this is not the case, some divergences or inconsistent mixes starts to shows up.

I readed somewhere an article of a guy that named his methods like "testShouldCreateCustomer", and for this reason he decided to drop the "test" prefix. But in fact he wasn't prefixing with "test", he was using "testShould" and changed to "should". Obviously, this did not convinced me.

I am personally strongly inclined to stick to "test" prefix because the methods names normally starts with verbs in the infinitive form ("get", "set", "add", "remove", "clear", "send", "receive", "open", "close", "read", "write", "create", "list", "pop", "print", etc, so is "test"). So, prefixing a method name with "should" makes it sound really very strange for me, looks wrong.

So, what is the real good reason to use "should" instead of "test"? What are the great advantages and disadvantages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

花之痕靓丽 2024-10-15 19:25:39

“应该”约定与行为驱动开发测试风格保持一致。

我个人非常喜欢用这种风格编写测试,因为它鼓励您编写作为规范来读取的测试,并且更符合类或系统的行为你正在测试。

在可能的情况下,我有时会更进一步,使用测试类的名称为测试类提供更多上下文:

class ANewlyCreatedAccount {
  shouldHaveAZeroBalance() {}
  shouldCalculateItsOwnInterest() {}
}

通过命名您的类并以这种规范风格思考它们,这可以为您提供很多关于要编写哪些测试的指导,以及按照什么顺序您应该编写测试并将其设置为绿色。

是的,“应该”与“测试”只是一个前缀,保持一致很重要,但这个问题还涉及如何测试代码和选择要编写的测试的风格和心态。 BDD 有很大的价值,所以我建议进一步阅读并尝试一下。

The 'Should' convention is aligned with the behaviour driven development style of testing.

I personally really prefer to write tests in this style, as it encourages you to write tests that read as specifications, and are more aligned with the behaviour of the class or system that you are testing.

Where possible, I sometimes go one step further and give the test class even more context using it's name:

class ANewlyCreatedAccount {
  shouldHaveAZeroBalance() {}
  shouldCalculateItsOwnInterest() {}
}

By naming your classes and thinking about them in this specification style, this can give you a lot of guidance on which tests to write, and in which order you should write the tests and make them green.

Yes, 'should' vs 'test' is just a prefix and it's important to be consistent, but this question is also about the style and mindset of how you test your code and choose which tests to write. BDD has a ton of value, so I suggest reading further and give this a try.

冰火雁神 2024-10-15 19:25:39

我想说,“测试”前缀只是需要时的预注释日的保留。我建议您只需为测试用例使用有意义的名称(这可能意味着有或没有“测试”)。

我更喜欢为测试方法命名,以便清楚正在测试的内容。即

checkNullParameter()
runSimpleQuery()
runQueryWithBadParam()

所有测试用例无论如何都位于测试目录中,并且所有实际测试都带有注释,因此“test”前缀相当多余。

I would say the 'test' prefix is simply a holdover from the pre annotation days when that was required. I would suggest you simply use meaningful names for your test cases (and that may mean with or without 'test').

I prefer to name the test method so that it is clear what is being tested. i.e.

checkNullParameter()
runSimpleQuery()
runQueryWithBadParam()

All the test cases are located in a test directory anyway and all actual tests are annotated, so the 'test' prefix is rather redundant.

感性不性感 2024-10-15 19:25:39

在命名问题上,一致性比正确性更重要。如果项目有任何问题,负责该项目的技术成员应该正式概述编码实践,这样这样的问题就不会浪费宝贵的项目时间。

Consistency is more important than being correct on naming issues. If there is any question on a project, the technical member responsible for the project should outline the coding practices formally so that issues like this don't kill valuable project time.

以酷 2024-10-15 19:25:39

在最初的 JUnit 中,测试方法必须从 test 开始。许多其他语言的框架都复制了这个约定。即使在 JUnit 中不再是这种情况,即使其他框架可能有所不同,我认为大多数程序员仍然非常熟悉名为 testX 的方法作为单元测试,所以我认为最好因此,请遵守 test 约定。

In the original JUnit, test methods had to begin test. A lot of frameworks for other languages copied this convention. Even though it's no longer the case in JUnit, and even though other frameworks may be different, I think most programmers are still pretty familiar with methods named e.g. testX as being unit tests, so I think it's good to stick to the test convention for that reason.

百善笑为先 2024-10-15 19:25:39

我更喜欢 test 后缀。您的项目中可能有一个带有 should 前缀的方法,例如 shouldBuy 并且您的测试将被称为 testShouldBuy 因为 shouldShouldBuy 看起来会很奇怪。

我还使用 MoreUnit Eclipse 插件,它将自动创建一个前缀为 Ctrl+U 时 >test 或当我按 Ctrl+J 时跳转到测试方法。 (尽管您可以配置它使用的前缀。)如果您的命名不一致,MoreUnit 等自动化工具将无法帮助您进行测试。

I prefer the test suffix. It's possible that you might have a method with a prefix of should in your project e.g. shouldBuy and your test would then be called testShouldBuy because shouldShouldBuy would just look very strange.

I also use the MoreUnit Eclipse plugin which will automatically create a test method prefixed with test when I press Ctrl+U or jump to the test method when I press Ctrl+J. (Although you can configure which prefix it uses.) If you are not consistent with your naming, automated tools such as MoreUnit won't be able to help you with your testing.

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