In this post, I will explain 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.

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.

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.

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.

testingproject

Project’s view

Test output after the test run: