Monday, 21 June 2021

Junit 5: @Tag: Declare a tag for annotated test class or test method

@Tag is a repeatable annotation that is used to declare a tag for the annotated test class or test method.

 

What is Repeatable annotation?

As the name suggests, Repeatable annotation can be applied multiple times to a declaration.

 

Example

@Test
@Tag("Speed")
@Tag("Fast")
@Tag("Performance")
public void perfTest() {
	assertTrue(true, "Performance tests failed");
}

 

For example, @Tag annotation is applied on test method perfTest three times.

 

Why do we tag the tests?

Tags are used to filter which tests are executed for a given test plan. For example, you may want to run only performance tests on every release, you can do that by tagging all the performance related tests.

 

Example

@Test
@Tag("Speed")
@Tag("Fast")
@Tag("Performance")
public void perfTest() {
	assertTrue(true, "Performance tests failed");
}

 

Following are the syntax rules for tags

a.   A tag must not be blank.

b.   A trimmed tag must not contain whitespace.

c.    A trimmed tag must not contain ISO control characters.

d.   A trimmed tag must not contain any of the following reserved characters.

1.   ,: comma

2.   (: left parenthesis

3.   ): right parenthesis

4.   &: ampersand

5.   |: vertical bar

6.   !: exclamation point

Find the below working example.

 

TagAnnotationDemo.java

package com.sample.app;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

public class TagAnnotationDemo {

	@Test
	@Tag("Authentication Test")
	@Tag("Auth Test")
	public void authTest() {
		assertTrue(true, "Authentication tests failed");
	}

	@Test
	@Tag("Speed")
	@Tag("Fast")
	@Tag("Performance")
	public void perfTest() {
		assertTrue(true, "Performance tests failed");
	}
}

 

You can download complete working application from this link.

https://github.com/harikrishna553/junit5/tree/master/junit5-examples

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment