NUnit and MSTest attributes


Previously, I had created this cheat sheet entry for myself. I am now working for a company that uses the Microsoft Unit Testing framework, so I decided to create an updated sheet so I would have one place to look up both frameworks.

          NUNIT

[SetupFixture]
public class NamespaceSetup
{
 [SetUp]
 BeforeAnyTestsInNamespace()
 {...}
 
 [TearDown]
 AfterAnyTestsInNamespace()
 {...}
}
 
[TestFixture]
public class ObjectUnitTests
{
  
 
 
  
 [OneTimeSetup]
 public void BeforeAnyTests()
 {...}
 
 
 [SetUp]
 public void BeforeEachTest()
 {...}

 [Test]
 [Category("group")]
 public void AUnitTest()
 {
  // Arrange 
  ... 
 
  // Act
  … 
 
  // Assert
  ...
 }
 
 [Ignore]
 public void NeverRunThis()
 {...}
 
 [Explicit]
 public void UserMustRequest()
 {...}
 
 [TearDown]
 public void AfterEachTest()
 {...}
 
 [OneTimeTearDown]
 public void AfterAllTests()
 {...}
 
  
 
 
}

          MSTest V2
 
 
 
 
 
 
 
 
 
 
 
 
 
[TestClass]
public class ObjectUnitTests
{
 [AssemblyInitialize]
 BeforeAnyTestsInAssembly()
 {...}
 
 [ClassInitialize]
 public void BeforeAnyTests(
  TestContext context)
 {...} 

 [TestInitialize]
 public void BeforeEachTest()
 {...}

 [TestMethod]
 [TestCategory("group")]
 public void AUnitTest()
 {
  // Arrange
  …
 
  // Act 
  …

  // Assert
  ...
 } 
 
 [Ignore("Not always run")] 
 public void NeverRunThis()
 {...}
 
 
  
 
  
 [TestCleanup]
 public void AfterEachTest()
 {...}
 
 [ClassCleanup]
 public void AfterAllTests()
 {...}
 
 [AssemblyCleanup]
 RunAfterAllTestsInAssembly()
 {...}
}

For data-driven unit testing:

 
[TestCase(d1,...,dN)]
[TestCase(d1,...,dN)]…
public void AUnitTest(
  T1 p1,...,TN pn) {…}

[DataTestMethod]
[DataRow(d1,...,dN)]
[DataRow(d1,...,dN)]…
public void AUnitTest(
  T1 p1,...,TN pn) {…}

[Test, TestCaseSource(
 nameof(SourceMethod1))]
public void TestParamsAreVariousTypes(
 T1 p1,...,TN pn)
{…}
 
 
 
 
 
static object[] SourceMethod1
{
  new object[] {d1,...,dN};
  new object[] {d1,...,dN};
  ...
}
 
 
 
 
 
 
 
 
 
 
 
 
 
[Test, TestCaseSource(
 nameof(SourceMethod2))]
public void TestAllParamsSameType(
 T p1,...,T pn)
{…}
 
static object[] SourceMethod2
{
 new T[] {d1,...,dN};
 new T[] {d1,...,dN};
 …
}
 
[Test, TestCaseSource(
 nameof(SourceMember))]
public void TestSingleParam(
 T p) 
{…}

static T[] SourceMember =
 new T[] {d1,...,dN};
[DataTestMethod]
[CustomDataSource]
public void TestParmsAreVariousTypes(
 T1 p1,...,TN pn)
{…}
 
public class CustomDataSourceAttribute :
 Attribute, ITestDataSource
{
 public IEnumerable
  GetData(MethodInfo mi)
 {
  yield return new object[]
   {d1,…,dN};
  yield return new object[]
   {d1,…,dN};
  …
 }
 public string GetDisplayName(
  MethodInfo mi, object[] d)
 {
  return (d == null)
   ? null
   : $"{mi.Name} - {formatting for d}";
 }
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

And MS Test has even more cumbersome ways of setting up data-driven tests

 
 
  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

[DataTestMethod]
[DynamicData(
 nameof(SourceMethod1), DynamicDataSourceType.Method)]
public void TestParamsAreVariousTypes(
 T1 p1,...,TN pn)
{…}

static IEnumerable<object[]> SourceMethod1()
{
 yield return new object[]
  {d1,...,dN};
 yield return new object[]
  {d1,...,dN};
 ...
}
 
[DataTestMethod]
[DynamicData(
 nameof(SourceProperty), DynamicDataSourceType.Property)]
public void AUnitTest2(
 T1 p1,...,TN pn) {…}

public static IEnumerable<object[]> SourceProperty
{
 get
 {
  yield return new object[]
   {d1,...,dN};
  yield return new object[]
   {d1,...,dN};
  ...
 }
}

As you can tell, my preference is for NUnit.

Advertisement

One thought on “NUnit and MSTest attributes”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s