Table of Contents
How to Run Test Suite in TestNG with Surefire Plugin
What we have learned so far?
Part 1 – What Is Testng? How To Configure Testng In Eclipse?
https://www.onlyfullstack.com/what-is-testng-how-to-configure-testng-in-eclipse/
https://www.onlyfullstack.com/testng-annotations/
What is TestNG Suite?
TestNG enables you to run test methods, test classes and test cases in parallel inside your project. By performing parallel execution, we can reduce the ‘execution time’ as tests are started and executed simultaneously in different threads.
Here we will see how to run multiple classes (aka different suites) using TestNG.
1. Write some test case files
Lets write two test files as below
TestNGAnnotationsTest.java
package com.onlyfullstack.service; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterGroups; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class TestNGAnnotationsTest { @BeforeGroups(groups = "sample") public void beforeGroups() { System.out.println("@BeforeGroups"); } @AfterGroups(groups = "sample") public void afterGroups() { System.out.println("@AfterGroups"); } @BeforeClass public void beforeClass() { System.out.println("@BeforeClass"); } @AfterClass public void afterClass() { System.out.println("@AfterClass"); } @BeforeMethod public void beforeMethod() { System.out.println("@BeforeMethod"); } @AfterMethod public void afterMethod() { System.out.println("@AfterMethod"); } @Test(groups = "sample") public void runTest1() { System.out.println("@Test - runTest1"); } @Test public void runTest2() { System.out.println("@Test - runTest2"); } @Test public void firstTest() { String sample = "Employee"; Assert.assertEquals("EMPLOYEE", sample.toUpperCase()); System.out.println("@Test - firstTest"); } }
EmployeeDaoLayerTest.java
package com.onlyfullstack.dao; import org.testng.Assert; import org.testng.annotations.Test; public class EmployeeDaoLayerTest { @Test public void testEmployeeData() { System.out.println("@Test - testEmployeeData"); } @Test public void testInsert() { System.out.println("@Test - testInsert"); } @Test public void testUpdate() { Assert.assertEquals("sample", "onlyfullstack"); } }
SampleDaoTest.java
package com.onlyfullstack.dao; import org.testng.annotations.Test; public class SampleDaoTest { @Test public void testConnection() { System.out.println("@Test - testConnection"); } @Test public void testSchema() { System.out.println("@Test - testSchema"); } @Test public void testDML() { System.out.println("@Test - testDML"); } }
2. Create a testng-suite.xml
You can give any name to this xml file. this file is used hold the suite information. So our file structure would be as below –
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite verbose="1" name="Onlyfullstack Suite" parallel="tests" thread-count="1"> <test name="TestNG Annotations"> <classes> <class name="com.onlyfullstack.service.TestNGAnnotationsTest"/> </classes> </test> <test name="Dao Layer Testing"> <classes> <class name="com.onlyfullstack.dao.SampleDaoTest"/> <class name="com.onlyfullstack.dao.EmployeeDaoLayer"></class> </classes> <!-- <packages> we can also define the package <package name="com.onlyfullstack.dao.*"/> </packages> --> </test></suite>
Lets understand this file –
1. thread-count: This is used for parallel execution, based on the number script. It will execute in parallel or sequential order(if the thread-count is 1).
2. verbose: It is used to log the execution details in the console. The value should be 1-10. The log details in the console window will get more detailed and clearer as you increase the value of the verbose attribute in the testng.xml configuration file.
3. name: Name of the suite. Here it is “Gmail Suite”
4. Parallel: To run scripts parallel, value can be tests/classes/methods/suites. Default value is none. parallel can accept different arguments as below –
parallel=”methods”: TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.
parallel=”tests”: TestNG will run all the methods in the same tag in the same thread, but each tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.
parallel=”classes”: TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.
parallel=”instances”: TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.
Additionally, the attribute thread-count allows you to specify how many threads should be allocated for this execution.
You can run the test suite as below
A. In Eclipse or IntelliJ
This will show the test case run results as below –
B. mvn
You will have to add the surefire plugin to run the test cases with testing-suite.xml file. Make sure you provide the correct testing-suite.xml path in the <suiteXmlFile> tag. Lets Configure the surefire plugin with testing to run the test cases from men and to generate the report.
3. How to configure the SureFire Plugin with TestNG?
Run Testng Suite in TestNG
add below <build> tag into pom.xml. this tag contains the surefire plugin which is responsible for the report generation. `Surefire plugin takes the <suiteXmlFile> tase which takes the path of the suite xml file.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>testng-tutorial</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.1.0</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.21.0</version> <configuration> <suiteXmlFiles> <suiteXmlFile>src/test/testng-suite.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> </project>
Now run the mvn clean test or mvn clean install command to trigger the test phase which will also call the surefire plugin to generate the test report for us.
the test phase will run the test cases from the testing-suite.xml file. Go to target/surefire-reports folder and open the emailable-report.html file to view the test cases report.
Surefire Test Report
Lets go to our next tutorial where we will discuss below points :
Part 4 – TestNG Groups Test
– What is TestNG Group?
– How to specify and run the TestNG group?
https://www.onlyfullstack.com/testng-groups-test/
Source Code
You can find the source code on below link –
https://github.com/onlyfullstack/testng-tutorial
TestNG Tutorial
https://www.onlyfullstack.com/testng-tutorial-for-beginners/