Introduction
Learn how to categorize unit test methods which is beneficial when there are many test methods and or more than one category.
Note
The information presented only touches the surface, see the Microsoft TechNet article in the see also section for more details.
Jumping in
Imagine having several hundred test methods that might be broken down into several categories, such as a category for data operations, another for API and so forth. In Visual Studio Test Explorer there are options to group test methods.
To use the trait option add the TestCategory attribute to a test method as shown below for MSTest.
[TestClass]
public partial class Dummy : TestBase
{
[TestMethod]
[TestCategory("EntityFrameworkCore")]
public void SomeTest()
{
}
}
The same in NUnit
[Test, Category("EntityFrameworkCore")]
public void SomeTest()
{
}
Slight problem
This allow a developer to group by traits now yet there is an issue ready to bite the developer which is, what happens if the category name changes? The developer must go through all test and rename the category.
What can work for any unit test library is to create an enum with categories which if renamed can be refactored to all instances by Visual Studio.
public enum Trait
{
PlaceHolder,
EntityFrameworkCore,
Configurations,
Controller,
Services,
StringHelpers,
GenericMath,
TimeOnly
}
For NUnit
[Test, Category(nameof(Trait.StringHelpers))]
For MSTest
[TestMethod, TestCategory(nameof(Trait.StringHelpers))]
Consider creating a class project which stores the enumerations and place the binary in a folder easy to get too or better yet a local NuGet package.
Simple local feed
Create a package
Create a folder e.g. C:\NuGetLocalFeed
Under Visual Studio options for NuGet add the folder
Now when the package is needed, select the local feed. For a more complete method to create a local feed see my Code magazine article Working with NuGet Local Packages.
MSTest specific option
Using the enumeration above, add the following class to the class project or in a unit test project.
/// <summary>
/// Declarative class for using Trait enum about for traits on test method.
/// </summary>
public class TestTraitsAttribute : TestCategoryBaseAttribute
{
private readonly Trait[] _traits;
public TestTraitsAttribute(params Trait[] traits)
{
_traits = traits;
}
public override IList<string> TestCategories => _traits.Select(trait
=> Enum.GetName(typeof(Trait), trait)).ToList();
}
Now a developer can setup a test method as shown below.
[TestMethod, TestTraits(Trait.StringHelpers)]
If this is appealing, see the project UnitTestHelperLibrary1 in the provided source code.
See also
My Microsoft TechNet article
Visual Studio: Different methods to display unit test
Source code
Clone the following GitHub repository.
There are two unit test projects, one for MSTest and one for NUnit.
Class projects
- DataLibrary to provide a backend for testing EF Core
- TeamLibrary, several methods for the test methods
- UnitTestHelperLibrary contains enum mentioned above
- UnitTestHelperLibrary1 for MSTest only
Bonus
Check out NaturalStringComparer method in TeamLibrary project for performing a natural sort on strings.
[TestMethod, TestCategory(nameof(Trait.StringHelpers))]
public void NaturalStringSort()
{
// arrange
List<string> fileNames = new()
{
"Example12.txt", "Example1.txt", "Example2.txt",
"Example3.txt", "Example4.txt", "Example10.txt"
};
List<string> expected = new()
{
"Example1.txt", "Example2.txt", "Example3.txt",
"Example4.txt", "Example10.txt", "Example12.txt"
};
// act
fileNames.Sort(new NaturalStringComparer());
// assert
CollectionAssert.AreEquivalent(fileNames, expected);
}
Top comments (0)