Wednesday 23 June 2021

Junit5: DisplayName: Declare custom display name to test class and test method

@DisplayName annotation is used to set custom display name to a test class and test method. Display name may contain spaces, special characters, and even emoji.

@DisplayName("Testing Login Page")
public class LoginPageTest {

	@Test
	@DisplayName("Invalid user name and password")
	public void invalidUserNameAndPassword() {
		assertTrue(true);
	}

	........
	........
}

 

Where the DisplayName useful?

Display names are typically used for test reporting in IDEs and build tools.

 

LoginPageTest.java

package com.sample.app;

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

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

@DisplayName("Testing Login Page")
public class LoginPageTest {

	@Test
	@DisplayName("Invalid user name and password")
	public void invalidUserNameAndPassword() {
		assertTrue(true);
	}

	@Test
	@DisplayName("Invalid user name and empty password")
	public void invalidUserNameAndEmptyPassword() {
		assertTrue(true);
	}

	@Test
	@DisplayName("Empty user name and empty password")
	public void emptyUserNameAndEmptyPassword() {
		assertTrue(true);
	}

	@Test
	@DisplayName("Correct user name and correct password")
	public void correctUserNameAndCorrectPassword() {
		assertTrue(true);
	}
}

 

When you ran the tests in class ‘LoginPageTest’, you will see the messages with display names in junit window.

 


 

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