TestNG Ignore Test - TestNG

How to ignore a test in TestNG?

In some instances, the code might not be ready and the test case meant for the test happens to fail. In such situations the test case is disabled by the annotation @Test(enabled = false)

The test case which is not ready for the test is bypassed by the annotation @Test(enabled = false), if the test method is annotated.

The practical implementation of @Test(enabled = false) is as follows:

Create a Class

  • A java class that is tested, MessageUtil.java is created in C:\>TestNG_WORKSPACE.

/*
* This class prints the given message on console.
*/

public class MessageUtil {

private String message;

//Constructor
//@param message to be printed
public MessageUtil(String message){
this.message = message;
}

// prints the message
public String printMessage(){
System.out.println(message);
return message;
}

// add "Hi!" to the message
public String salutationMessage(){
message = "Hi!" + message;
System.out.println(message);
return message;
}
}

Create Test Case Class

  • A java test class, IgnoreTest.java is created.
  • The test methods testPrintMessage(), and, testSalutationMessage() are added to the class.
  • An Annotation @Test(enabled = false) is added to the method testPrintMessage().

A java class file IgnoreTest.java is created in C:\>TestNG_WORKSPACE.

import org.testng.Assert;
import org.testng.annotations.Test;

public class IgnoreTest {
String message = "Manisha";
MessageUtil messageUtil = new MessageUtil(message);

@Test(enabled = false)
public void testPrintMessage() {
System.out.println("Inside testPrintMessage()");
message = "Manisha";
Assert.assertEquals(message, messageUtil.printMessage());
}

@Test
public void testSalutationMessage() {
System.out.println("Inside testSalutationMessage()");
message = "Hi!" + "Manisha";
Assert.assertEquals(message, messageUtil.salutationMessage());
}
}

Create testng.xml

To execute the test cases, a testng.xml is created in C:\>TestNG_WORKSPACE

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite1">
<test name="test1">
<classes>
<class name="IgnoreTest" />
</classes>
</test>
</suite>

By using javac, MessageUtil and test case classes are compiled.

C:\TestNG_WORKSPACE>javac MessageUtil.java IgnoreTest.java

Testing.xml is run which stops running the testPrintMessage(), the test case that is defined in the Test Case class.

C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

The output is verified. The test case testPrintMessage() is not tested.

Inside testSalutationMessage()
Hi!Manisha

===============================================
Suite1
Total tests run: 1, Failures: 0, Skips: 0
===============================================

The group of test can be ignored and the reason for the same is explained in the next chapter.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

TestNG Topics