A Clockwork Engineer

Attributes and Annotations of Testing Frameworks

Frequently used attribute 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 about frequently used ones to run test scenarious properly. You would find comparisons of NUnit, MSTest, xUnit.net and JUnit testing frameworks down below.

You could find very comprehensive tutorials on Guru99 website.

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

There is a 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 its category on your test runner tool or run them in a Continuous Integration application seperately.

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

testingIgnore.png Display on Test Explorer

testingIgnoreJenkins.png Display on Jenkins CI Test Coverage Report

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

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

1
2
3
4
5
6
7
8
9
10
namespace OtomatikMuhendis.TestSample
{
  public class DivideClass
  {
    public static int DivideMethod(int numerator, int denominator)
    {
      return (numerator / denominator);
    }
  }
}

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

Replace the unit test class with the following example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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.png Project will be like this

Output of our test run;

AssemblyInit DivideMethod_DivideByOne_ResultIsEqual
ClassInit DivideMethod_DivideByOne_ResultIsEqual
TestMethodInit
TestMethod_DivideMethod_DivideByOne_ResultIsEqual
TestMethodCleanup
TestMethodInit
TestMethod_DivideMethod_DivideByTwo_ResultIsHalf
TestMethodCleanup
ClassCleanup
AssemblyCleanup
test, nunit, xunit, mstest, junit Testing

Author: Olcay Bayram

A software enthusiast; currently a Senior Software Engineer of a global retail company, based in Amsterdam. Apart from the BSc, he holds a masters in IT.