26
Dec

Selective and Prioritized testmethod execution with TestNG

TestNG provides features to allow selective execution of testcases based on userdefined priority. Sometimes we do not need to execute all test methods as defined in multiple test classes.

TestNG supports selective execution of test methods by “enabled” keyword. TestNG allows prioritized execution i.e. testmethod1 first then testmethod2 by “priority” keyword.

We can use following code snippets to perform Selective, Prioritized execution of TestNG methods.

Step 1: Create testmethods with “priority” and “enabled” keyword

@Test(groups = { “smoke” }, priority=1, enabled=false)
public void testmethodOne() {
System.out.println(“testmethod1”);
}

@Test(groups = { “smoke” }, priority=1, enabled=true)
public void testmethodTwo() {
System.out.println(“testmethod2”);
}

@Test(groups = { “smoke” }, priority=2, enabled=true)
public void testmethodThree() {
System.out.println(“testmethod3”);
}

Step 2: Execute testmethods

TestNG will execute testmethod2 and testmethod3 with following order
testmethod2 -> testmethod3