I have some simple extension methods in c# I'm writing unit tests against. 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. I don't want any assumptions made in the tests that the overloads are chained and pass-through.
How would you approach this?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
I tend to write test names in a more Given, When , Then way. 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()
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. So, as a simple example, assume I have a Stack class like so:
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. 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. 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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With