- parametrize tests
- run tests in separate sets grouped by classes or methods
- run tests in multiple threads
- generate test reports
- use annotations
- and more..
The installation and getting started with testng is very well described on their site testng.org - shortly:
- Open Eclipse
- Help > Install New Software...
- Add http://beust.com/eclipse repository and complete the proccess
- Window > Show View > Other.. select Java > TestNG > OK
- Create a new TestNG class for existing package (test): right-click on the package > New > TestNG class (name it TestExample)
- Copy the following code to the class
package test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.Parameters;
public class TestExample {
import org.testng.annotations.Test;
import org.testng.annotations.Parameters;
public class TestExample {
@BeforeTest
@Parameters({"usrName", "pwd"})
public void setUp(String
usrName, String pwd) {
// code that will be executed before tests
}
}
@Test
public void test1() {
// test1 steps
System.out.println("test1 passed");
System.out.println("test1 passed");
}
@Test
public void test2() {
// test2 steps
System.out.println("test2 passed");
System.out.println("test2 passed");
}
}
7. Modify the XML configureation (testng.xml by default) as follows
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="TestName" preserve-order="true">
<parameter name="usrName" value="test"/>
<parameter name="pwd" value="1234"/>
<classes>
<class name="test.TestExample">
<methods>
<include name="test1" />
<include name="test2" />
</methods>
</class>
</classes>
</test>
</suite>
8. Run testng.xml
Console results:
[TestNG] Running:
C:\workspace\webdriver\testng.xml
test1 passed
test2 passed
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
TestNG results: