The Maven Surefire Plugin can be used to run testng.xml files.
Refer to the official documentation: Maven Surefire Plugin - TestNG.
Add Plugin and Dependency to pom.xml
Add the Maven Surefire plugin configuration and TestNG dependency:
xml<plugins>
<!-- Other plugins -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
Add TestNG dependency:
xml<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
Step 8: Create TestNG Classes and XML
Create classes in the com.birla package:
javapackage com.birla;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
public class AndroidTest {
@Test
public void testB() {
System.out.println("Calling Android Test");
assertTrue(true);
}
}
javapackage com.birla;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
public class MobileTest {
@Test
public void testA() {
System.out.println("Calling Mobile Test");
assertTrue(true);
}
}
javapackage com.birla;
import org.testng.annotations.Test;
class iosTest {
@Test
public void testC() {
System.out.println("hi IOS Test");
}
}
Create testng.xml:
xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test">
<classes>
<class name="com.birla.iosTest"/>
<class name="com.birla.MobileTest"/>
<class name="com.birla.AndroidTest"/>
</classes>
</test>
</suite>
Step 9: Run testng.xml File from pom.xml
Add the relative path to testng.xml in pom.xml:
xml<plugins>
<!-- Other plugins -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/Resources/Runner/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
Step 10: Run Maven Test
Run as Maven test.
Homework:
- Create a
testng.xmland run it using Maven.
By following these steps, you will successfully create, configure, and run a Maven project with TestNG tests using the Maven Surefire plugin.
No comments:
Post a Comment