Annotations and Attributes of Testing Frameworks

In this post, I will explain frequently used attributes and annotations in various testing frameworks.

It is called attributes in the .NET environment and annotations in Java. We use them to declare information about methods, types, properties, and so on.

We will discuss frequently used ones to run test scenarios properly. You would find comparisons of NUnit, MSTest, xUnit.net, and JUnit testing frameworks down below.

NUnit MSTest xUnit.net JUnit 4 Junit 5 Description
[TestFixture] [TestClass] This indicates that the class has test methods.
[Test] [TestMethod] [Fact] @Test @Test Marks a test case.
[OneTimeSetUp] [ClassInitialize] IClassFixture @BeforeClass @BeforeAll The one-time triggered method before test cases start.
[OneTimeTearDown] [ClassCleanup] IClassFixture @AfterClass @AfterAll The one time triggered method after test cases end.
[SetUp] [TestInitialize] Constructor @Before @BeforeEach Triggered before every test case.
[TearDown] [TestCleanup] IDisposable.Dispose @After @AfterEach Triggered after every test case.
[Ignore] [Ignore] [Fact(Skip=”reason”)] @Ignore @Disabled Ignores the test case.
[Category(“”)] [TestCategory(“”)] [Trait(“Category”, “”)] @Category(*.class)   Categorizes the test cases or classes.

There is little difference between them except that the xUnit framework. xUnit prefers inheritance for the ones that it doesn’t want to be used very often.

Category separation is a best practice on testing because you could see your test cases by their category on your test runner tool or run them in a Continuous Integration application separately.

If you don’t want to run a test temporarily you could use ignore the attribute. Your test runner tool will skip that test and show it with the provided message.

annotations
Display on Test Explorer
testingignorejenkins
Display on Jenkins CI Test Coverage Report

Let’s build a test project to see the order of the attributes precisely.

Create a new Class Library project and a method to test like the one below.

namespace OtomatikMuhendis.TestSample
{
  public class DivideClass
  {
    public static int DivideMethod(int numerator, int denominator)
    {
      return (numerator / denominator);
    }
  }
}

Add a Unit Test Project to the Solution and add a reference of the class library project.

Replace the unit test class with the following example.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OtomatikMuhendis.TestSample;
using System.Diagnostics;

namespace OtomatikMuhendis.UnitTest
{
  [TestClass]
  public sealed class DivideClassTest
  {
    [AssemblyInitialize]
    public static void AssemblyInit(TestContext context)
    {
      Debug.WriteLine("AssemblyInit " + context.TestName);
    }

    [ClassInitialize]
    public static void ClassInit(TestContext context)
    {
      Debug.WriteLine("ClassInit " + context.TestName);
    }

    [TestInitialize]
    public void Initialize()
    {
      Debug.WriteLine("TestMethodInit");
    }

    [TestMethod]
    [TestCategory("MathLibTests")]
    public void DivideMethod_DivideByOne_ResultIsEqual()
    {
      //Arrenge
      var numerator = 6;
      var denominator = 1;

      //Act
      var result = DivideClass.DivideMethod(numerator, denominator);

      //Assert
      Assert.AreEqual(numerator, result);

      Debug.WriteLine("TestMethod_DivideMethod_DivideByOne_ResultIsEqual");

    }

    [TestMethod]
    [TestCategory("MathLibTests")]
    public void DivideMethod_DivideByTwo_ResultIsHalf()
    {
      //Arrenge
      var numerator = 6;
      var denominator = 2;

      //Act
      var result = DivideClass.DivideMethod(numerator, denominator);

      //Assert
      Assert.AreEqual(numerator, result * denominator);

      Debug.WriteLine("TestMethod_DivideMethod_DivideByTwo_ResultIsHalf");

    }

    [TestMethod]
    [TestCategory("MathLibTests")]
    [ExpectedException(typeof(System.DivideByZeroException))]
    [Ignore]
    public void DivideMethod_DivideByZero_ThrowsDivideByZeroException()
    {
      //Arrenge
      var numerator = 6;
      var denominator = 0;

      //Act
      var result = DivideClass.DivideMethod(numerator, denominator);

      //Assert
      Assert.AreEqual(numerator, result * denominator);

      Debug.WriteLine("TestMethod_DivideMethod_DivideByZero_ThrowsDivideByZeroException");

    }

    [TestCleanup]
    public void Cleanup()
    {
      Debug.WriteLine("TestMethodCleanup");
    }

    [ClassCleanup]
    public static void ClassCleanup()
    {
      Debug.WriteLine("ClassCleanup");
    }

    [AssemblyCleanup]
    public static void AssemblyCleanup()
    {
      Debug.WriteLine("AssemblyCleanup");
    }
  }
}

testingproject
Project’s view

Test output after the test run:

AssemblyInit DivideMethod_DivideByOne_ResultIsEqual
ClassInit DivideMethod_DivideByOne_ResultIsEqual
TestMethodInit
TestMethod_DivideMethod_DivideByOne_ResultIsEqual
TestMethodCleanup
TestMethodInit
TestMethod_DivideMethod_DivideByTwo_ResultIsHalf
TestMethodCleanup
ClassCleanup
AssemblyCleanup

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.