In this testng maven example, we will learn how to execute testng tests and suites using Maven.
1. Maven Dependency
Before diving into different configurations, let’s include the latest version of org.testng:testng in the project configuration.
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.6.1</version>
<scope>test</scope>
</dependency>
2. Include Maven-surefire-plugin
Next, we will need to include the Maven-surefire-plugin in the build section of the pom.xml.
- By default, the surefire plugin executes all tests in the directory
'src/test/java'that follow the default naming convention i.e. their names are of pattern*Test.java. - We can include and/or exclude the test classes using different configurations in the
suiteXmlFileselement of surefire configuration.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>
src\test\resources\hello_testng_suite.xml
</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
3. Creating Test Suites
A test suite is XML configuration file to group multiple related test classes. Suites help in testing a particular functionality by executing tests only related to that feature.
A typical suite XML file is given below:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="HelloTestNgSuite">
<test name="HelloWorldTest" preserve-order="true" thread-count="10" parallel="methods">
<classes>
<class name="com.howtodoinjava.demo.testng.HelloTestNg"/>
<class name="com.howtodoinjava.demo.testng.AnotherTest"/>
<class name="com.howtodoinjava.demo.testng.YetAnotherTest"/>
</classes>
</test>
</suite>
We can use regular expressions and various inclusion and expulsion patterns in the suite files.
4. Executing Tests and Suites
4.1. Execute All Tests and Suites
The simplest way to execute all testing tests is run the mvn test command:
$ mvn test
4.2. Execute a Specific Test Suite
The easiest and straight-forward way to execute only a single test class or test suite is by excluding/commenting out all unwanted suites from the configuration of maven-surefire-plugin and run the tests using mvn test command. This way, we do not need to setup extra classpath variables in the application and Maven takes care of everything.
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src\test\resources\hello_testng_suite.xml</suiteXmlFile>
<!-- Comment out suites that we do not want to run-->
<!--suiteXmlFile>src\test\resources\unwantedSuite.xml</suiteXmlFile-->
</suiteXmlFiles>
</configuration>
Assuming that we have TestNG and other dependent libraries in the classpath, we can run the one or more suite files (separated with a space) as follows:
$ java org.testng.TestNG testng1.xml [testng2.xml testng3.xml ...]
4.3. Execute a Specific Test Class
We can execute a specific test class directly from the command line:
$ mvn test -Dtest="com.howtodoinjava.demo.testng.HelloTestNg"
Happy Learning !!
Hi,
I do not have Maven installed locally on Win 10; using POM.xml and maven setup in eclipse for testng framework.
So, no mvn.bat does not exist on system.
How do I run the tests from cmd or batch now?
Regards,
Girish
You need to have maven installed locally if you want to run tests from cmd.
How we make sure necessary dependent files are being picked during test suite execution thru ‘testng.xml’?
Lets say, ‘testng.xml’ has details about a class to be executed. The tests in the class need some zip files which should be deployed on the target server for test to execute successfully. How to make sure we specify correct path so that zip files along with class are also picked for execution.
I am getting this error – Caused by: java.io.FileNotFoundException: null//.zip (No such file or directory). Not sure why it is picking up ‘null’
Hi Lokesh,
Thank you for your great work. I just want to ask you if i have a lot of xml suite files for the entire application like 50 files or more, should i wrote them all in the pom.xml so i can run them. Is there anyway i can run a folder where all my suite xml file are. it would be better.
Please i need your help on this.
Thanks !
Hi,
Thanks for the detailed post. It is very helpful.
I request your help in addressing one of the issues I am come across.
I have created a pom.xml file having the same contents as mentioned under the section : “Add the steps in Maven pom.xml” in this post.
I have placed the pom.xml file inside the directory of the project containing the testng.xml file (Project\Runlist directory) where as, the tests are located in : Project\src\test\java). I am hitting an error : cannot find class in classpath on running mvn test . I am finding bit difficult to understand the issue here. If I run the testng.xml from eclipse, (right click on testng.xml -> Run as -> testng), the test runs fine. Can you please help me in understanding the issue here?
More Details: My eclipse workspace contains 4 projects 1) framework, 2) Page objects 3) Page Operations and 4) Tests Tests under the Tests project are depending on the other 3 projects.
There is no other possible meaning of “cannot find class in classpath”. It simply means that required classes are not in classpath. Run the command again after updating the classpath.
Thanks Lokesh. I think the things are all right as the tests are running fine if I try to run from eclipse. But why Maven command complaining about class path??
Thanks,
Dinesh
It’s because eclipse include the classes folder path to classpath while running from eclipse automatically. If you are running from command prompt then you need to add path by your own.