Did you think Reporting is the only special feature of TestNG?
Let us assume the below scenario and write a test class for that.
Agni is going to drive a car. He has to start the engine first, put the first gear to move the vehicle then second gear and successive gears after gaining speed.
A simple test for the above mentioned scenario.
package seleniumWithTestNG;
import org.testng.annotations.Test;
public class TestWithPriority {
@Test
public void startEngine(){
System.out.println("Engine started");
}
@Test
public void putFirstGear(){
System.out.println("Car is in first Gear");
}
@Test
public void putSecondGear(){
System.out.println("Car is in second Gear");
}
@Test
public void putThirdGear(){
System.out.println("Car is in third Gear");
}
}
Run this program and below is the output that you will get.
Car is in first Gear
Car is in second Gear
Car is in third Gear
Engine started
PASSED: putFirstGear
PASSED: putSecondGear
PASSED: putThirdGear
PASSED: startEngine
===============================================
Default test
Tests run: 4, Failures: 0, Skips: 0
===============================================
Note that, the first, second and third gear has been excuted and then only the Engine is getting started. This is wrong. The above example is not controlled by us. It is executing with the alphabetical order. In most of the real world problems, we need to control the test executions. Let us rewrite the program to control the test execution.
package seleniumWithTestNG;
import org.testng.annotations.Test;
public class TestWithPriority {
@Test (priority=1)
public void startEngine(){
System.out.println("Engine started");
}
@Test (priority=2)
public void putFirstGear(){
System.out.println("Car is in first Gear");
}
@Test (priority=3)
public void putSecondGear(){
System.out.println("Car is in second Gear");
}
@Test (priority=4)
public void putThirdGear(){
System.out.println("Car is in third Gear");
}
}
We can set the priority. Priority 1 will be excuted first and then succesively. The out put is given below.
Engine started
Car is in first Gear
Car is in second Gear
Car is in third Gear
PASSED: startEngine
PASSED: putFirstGear
PASSED: putSecondGear
PASSED: putThirdGear
===============================================
Default test
Tests run: 4, Failures: 0, Skips: 0
===============================================