我应该更改单元测试的命名约定吗? [英] Should I change the naming convention for my unit tests?

查看:101
本文介绍了我应该更改单元测试的命名约定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前对单元测试使用一个简单的约定.如果我有一个名为"EmployeeReader"的类,则创建一个名为"EmployeeReader.Tests"的测试类.然后,我在测试类中为该类创建所有测试,其名称如下:

I currently use a simple convention for my unit tests. If I have a class named "EmployeeReader", I create a test class named "EmployeeReader.Tests. I then create all the tests for the class in the test class with names such as:

  • Reading_Valid_Employee_Data_Correctly_Generates_Employee_Object
  • Reading_Missing_Employee_Data_Throws_Invalid_Employee_ID_Exception

以此类推.

我最近一直在阅读有关不同类型的命名约定在BDD中使用.我喜欢这种命名的可读性,最终得到如下的测试列表:

I have recently been reading about a different type of naming convention used in BDD. I like the readability of this naming, to end up with a list of tests something like:

  • When_Reading_Valid_Employee(固定装置)
    • Employee_Object_Is_Generated(方法)
    • Employee_Has_Correct_ID(方法)
    • When_Reading_Valid_Employee (fixture)
      • Employee_Object_Is_Generated (method)
      • Employee_Has_Correct_ID (method)
      • An_Invalid_Employee_ID_Exception_Is_Thrown(方法)

      以此类推.

      有人使用过两种命名方式吗?您能否提供任何建议,好处,缺点,陷阱等,以帮助我决定是否为我的下一个项目切换?

      Has anybody used both styles of naming? Can you provide any advice, benefits, drawbacks, gotchas, etc. to help me decide whether to switch or not for my next project?

      推荐答案

      您的第二个示例(每个逻辑任务"都有一个固定装置,而不是每个类都有一个固定装置)具有您可以具有不同的SetUp和TearDown逻辑的优点.用于每个任务,从而简化了您自己的测试方法,并使它们更具可读性.

      Your second example (having a fixture for each logical "task", rather than one for each class) has the advantage that you can have different SetUp and TearDown logic for each task, thus simplifying your individual test methods and making them more readable.

      您不需要以一个或多个为标准.我们混合使用这两种方法,具体取决于每个类必须测试多少个不同的任务".

      You don't need to settle on one or the other as a standard. We use a mixture of both, depending on how many different "tasks" we have to test for each class.

      这篇关于我应该更改单元测试的命名约定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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