简体   繁体   English

c#unit test - 重载方法测试的命名约定

[英]c# unit test - naming convention for overloaded method tests

I have some simple extension methods in c# I'm writing unit tests against. 我在c#中有一些简单的扩展方法,我正在编写单元测试。 One of the extension methods is overloaded, so I'm having trouble coming up with a reasonable naming convention for the unit tests. 其中一种扩展方法是重载的,所以我在为单元测试提出合理的命名约定时遇到了麻烦。

Example overloaded method: 示例重载方法:

public static class StringExtensions
{
    public static List<string> ToList(this string value, char delimiter)
    {
        return ToList(value, new[] { delimiter });
    }

    public static List<string> ToList(this string value, char[] delimiterSet) { .. }
}

If I want to write two unit tests, one for each method that how would you name the tests? 如果我想编写两个单元测试,每个方法一个,你会如何命名测试? Normally I would use simply: 通常我会简单地使用:

[TestMethod, TestCategory("Single Method Tests")]
public void ToListTest

But with two methods of the same name, I'm struggling to find a good convention.. 但是有两个同名的方法,我很难找到一个好的约定..

[TestMethod, TestCategory("Single Method Tests")]
public void ToListTest

[TestMethod, TestCategory("Single Method Tests")]
public void ToListTestOverload

is horrendous. 太可怕了。

[TestMethod, TestCategory("Single Method Tests")]
public void ToListTestWithChar

[TestMethod, TestCategory("Single Method Tests")]
public void ToListTestWithCharArray

based on parameter type is better but still pretty terrible. 基于参数类型更好,但仍然非常可怕。

Some may suggest to write one test method that targets both overloads, but I ideally want to have a 1:1 relationship with unit tests and methods, even if the overloads are currently chained. 有些人可能会建议编写一种针对两种重载的测试方法,但理想情况下我希望与单元测试和方法保持1:1的关系,即使过载当前是链接的。 I don't want any assumptions made in the tests that the overloads are chained and pass-through. 我不希望在测试中做出任何超载被链接和传递的假设。

How would you approach this? 你会怎么做?

Roy Osherove in his book, The Art of Unit Testing recommends naming conventions based on the behavior you're trying to test and not necessarily having a one-to-one mapping between production methods and test methods. Roy Osherove在他的书“单元测试的艺术”中建议根据您尝试测试的行为命名约定,而不一定在生产方法和测试方法之间进行一对一的映射。 So, as a simple example, assume I have a Stack class like so: 所以,作为一个简单的例子,假设我有一个像这样的Stack类:

public class Stack
{
    public void Push(int value);
    public int Pop();
    public bool IsEmpty();
}

One way to write tests is to have three test methods: 编写测试的一种方法是使用三种测试方法:

[TestMethod]
public void PushTest

[TestMethod]
public void PopTest

[TestMethod]
public void IsEmptyTest

However, there's a better way to test that specifies the behaviors of the Stack class. 但是,有一种更好的测试方法可以指定Stack类的行为。 The idea then is that you have a one-to-one mapping between class behaviors and test methods. 接下来的想法是,您在类行为和测试方法之间进行一对一的映射。 So, one behavior is that IsEmpty on an empty stack returns true. 因此,一种行为是空堆栈上的IsEmpty返回true。 Another is that pushing one element makes the stack not empty. 另一个是推送一个元素使堆栈不为空。 I'd then write tests in the Arrange/Act/Assert format according to these plain English sentences specifying these behaviors in the format: 然后,我将根据这些简单的英语句子以Arrange / Act / Assert格式编写测试,以下列格式指定这些行为:

MethodName_StateOfTheObject_ExpectedResult

So for the behaviors mentioned above: 所以对于上面提到的行为:

[TestMethod]
IsEmpty_OnEmptyStack_ReturnsTrue

[TestMethod]
Push_OnEmptyStack_MakesStackNotEmpty

I would encourage you to think about the behaviors you're trying to test in on the methods above and then map those behaviors to tests. 我鼓励您考虑一下您尝试在上述方法中测试的行为,然后将这些行为映射到测试中。 For example: 例如:

[TestMethod]
ToList_OnEmptyString_ReturnsEmptyList

[TestMethod]
ToList_OnStringWithDelimiters_ReturnsListWithTokensSeparatedByDelemiter

[TestMethod]
ToList_OnStringWithoutDelimiters_ReturnsListWithOneString

More information here 更多信息在这里

I tend to go for the latter, though less based on the parameters being passed in and more on the actual functionality being tested. 我倾向于选择后者,尽管不是基于传入的参数,而是基于测试的实际功能。

So not: 所以不是:

  • TestFunctionWithListOfNumbers
  • and TestFunctionWithListOfStrings TestFunctionWithListOfStrings

but perhaps: 但也许:

  • TestFunctionWithStudentIDs
  • and TestFunctionWithStudentNames TestFunctionWithStudentNames

I tend to write test names in a more Given, When , Then way. 我倾向于以更多Given,When,Then方式编写测试名称。 This makes the test name verbose but it tells me what the function being tested was intending to do. 这使得测试名称变得冗长,但它告诉我正在测试的功能是什么。 So in your case i would have tests as 所以在你的情况下我会测试

[TestMethod]
public void GivenSingleCharDelimeter_ToList_Returnsxxx()
[TestMethod]
public void GivenCharArrayDelimeter_ToList_Returnsxxx()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM