TestNG with IntelliJ IDE
Tutorial 1: Setup environment
- Create a java project named
TestNG
- Add dependency
- Go to mvnrepository
- Search for
testng
- Download the jar of the latest version
- Add the jar dependency into the project structure
Tutorial 2: Creating & Run Tests using TestNG.xml
- Install TestNG xml plugin
- Invalid and restart IntelliJ
- Add another class case
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28// src/SecondTestCase.java
import org.testng.Assert;
import org.testng.annotations.Test;
public class SecondTestCase {
1) (priority =
public void setup() {
System.out.println("opening browser");
}
3) (priority =
public void searchCustomer() {
System.out.println("This is search customer test");
Assert.assertEquals(1, 2);
}
2) (priority =
public void addCustomer() {
System.out.println("This is add customer test");
}
4) (priority =
public void tearDown() {
System.out.println("closing browser");
}
} - Add TestNG XML file
- click project name,
New
->File
, addtestng.xml
- In
testng.xml
, add1
2
3
4
5
6
7
8<suite name="All Test Suite">
<test name="testngTest">
<classes>
<class name="FirstTestCase"/>
<class name="SecondTestCase"/>
</classes>
</test>
</suite>
- click project name,
- Click and run
testng.xml
- To add reports
Run
->Edit Configuration
- Click
testng.xml
->Listeners
- Click
+
addorg.testng.reporters.FailedReporter
andorg.testng.reporters.EmailableReporter
- Run
testng.xml
again and the reports will be inttest-output
folder
Tutorial 3: Annotations in TestNG
1 | <!-- a --> |
Annotation | When the method will be called |
---|---|
BeforeSuite | a |
BeforeTest | c |
BeforeClass | e |
BeforeMethod | g |
AfterMethod | h |
AfterClass | f |
AfterTest | d |
AfterSuite | b |
Tutorial 4: Prioritizing & Disabling Tests
Prioritizing
1 | // src/PriorityExample.java |
The priority
will set the order of tests
Disabling
1 | // src/PriorityExample.java |
Tutorial 5: Dependency Tests in TestNG & AlwaysRun property
dependsOnMethods | specifies the tests that the method depends on |
alwaysRun | alwaysRun = true forces the execution of the test even if its dependencies fail |
Tutorial 6: Grouping Tests
Define the groups
1
"...", "..."}) (groups = {
In
testng.xml
, define how to run the groups. E.g1
2
3
4
5
6
7
8
9
10
11
12
13
14<!-- testng.xml -->
<suite name="All Test Suite">
<test name="groupingTest">
<groups>
<run>
<include name="sanity"/>
<exclude name="regression"/>
</run>
</groups>
<classes>
<class name="GroupingExample"/>
</classes>
</test>
</suite>
- Use cases: test for different version
Tutorial 7: Assertions
Tutorial 8: Paramters from testng.xml
E.g:
- in
testng.xml
, add1
<parameter name="browser" value="chrome">
- before the method add
@Parameters
, and take the parameters as arguments1
2"browser"}) ({
public void testBrowser(String browser) {...}
Tutorial 9: DataProvider in TestNG
DataProvider and DataProviderClass in the same file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22// src/dataprovider.DataProviderExample.java
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderExample {
"LoginDataProvider") (name =
public Object[][] getData(){
Object[][] data = {
{"abc@gmail.com", "abc"},
{"xyz@gmail.com", "xyz"},
{"mno@gmail.com", "mno"},
};
return data;
}
"LoginDataProvider") (dataProvider =
// get the dataProvider by name
public void loginTest(String email, String password){
System.out.println("email: " + email + ", password: " + password);
}
}DataProvider and DataProviderClass in different files
1
2
3
4
5
6
7
8
9
10
11// src/dataprovider.DataProviderExample2.java
import dataprovider.DataProviderExample;
import org.testng.annotations.Test;
public class DataProviderExample2 {
"LoginDataProvider", dataProviderClass = DataProviderExample.class) (dataProvider =
// dataProviderClass denotes the location of dataProviderClass
public void loginTest(String email, String password){
System.out.println("email: " + email + ", password: " + password);
}
}
Tutorial 10: Parallel Tests
E.g.
- Create two test classes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51// src/parallel.ParallelExample1.java
import org.testng.annotations.Test;
public class ParallelExample1 {
public void test1() {
System.out.println("ParallelExample1 ... test1 ...");
}
public void test2() {
System.out.println("ParallelExample1 ... test2 ...");
}
public void test3() {
System.out.println("ParallelExample1 ... test3 ...");
}
public void test4() {
System.out.println("ParallelExample1 ... test4 ...");
}
}
// src/parallel.ParallelExample2.java
import org.testng.annotations.Test;
public class ParallelExample2 {
public void test1() {
System.out.println("ParallelExample2 ... test1 ...");
}
public void test2() {
System.out.println("ParallelExample2 ... test2 ...");
}
public void test3() {
System.out.println("ParallelExample2 ... test3 ...");
}
public void test4() {
System.out.println("ParallelExample2 ... test4 ...");
}
} - Set the testng xml
1
2
3
4
5
6
7
8
9<!-- testng3.xml -->
<suite name="All Test Suite">
<test name="parallelTest" parallel="classes" thread-count="2">
<classes>
<class name="parallel.ParallelExample1"/>
<class name="parallel.ParallelExample2"/>
</classes>
</test>
</suite> - Check the results:
1
2
3
4
5
6
7
8parallel.ParallelExample2 ... test1 ...
parallel.ParallelExample1 ... test1 ...
parallel.ParallelExample1 ... test2 ...
parallel.ParallelExample2 ... test2 ...
parallel.ParallelExample1 ... test3 ...
parallel.ParallelExample2 ... test3 ...
parallel.ParallelExample1 ... test4 ...
parallel.ParallelExample2 ... test4 ...
- parallel could be
"methods"
,"classes"
,"tests"
Tutorial 11: Listeners
E.g
Implement a customized listeners class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43// src/listeners/CustomListeners.java
package listeners;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
public class CustomListeners implements ITestListener {
public void onTestStart(ITestResult iTestResult) {
System.out.println("Start test..." + iTestResult.getName());
}
public void onTestSuccess(ITestResult iTestResult) {
System.out.println("Passed test..." + iTestResult.getName());
}
public void onTestFailure(ITestResult iTestResult) {
System.out.println("Failed test..." + iTestResult.getName());
}
public void onTestSkipped(ITestResult iTestResult) {
System.out.println("Skipped test..." + iTestResult.getName());
}
public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {
}
public void onStart(ITestContext iTestContext) {
System.out.println("Start Test execution..." + iTestContext.getName());
}
public void onFinish(ITestContext iTestContext) {
System.out.println("Finish Test execution..." + iTestContext.getName());
}
}Check the result of the customized listeners
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41// src/listeners/ListenerTest.java
package listeners;
import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
.class) (listeners.CustomListeners
// set the listeners
public class ListenerTest {
public void test1() {
System.out.println("This is test 1");
Assert.assertEquals("A", "A");
}
// STDOUT:
// Start test...test1
// This is test 1
// Passed test...test1
public void test2() {
System.out.println("This is test 2");
Assert.assertEquals("A", "B");
}
// STDOUT:
// Start test...test2
// This is test 2
// Failed test...test2
public void test3() {
System.out.println("This is test 3");
throw new SkipException("Skip test 3");
}
// STDOUT:
// Start test...test3
// This is test 3
// Skipped test...test3
}Another way to set the Listeners is in
testng.xml
1
2
3
4
5
6
7
8
9
10
11<!-- testng2.xml -->
<suite name="All Test Suite">
<listeners>
<listener class-name="listeners.CustomListeners"/>
</listeners>
<test name="listenersTest">
<classes>
<class name="listeners.ListenerTest"/>
</classes>
</test>
</suite>
Use cases:
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.