Sunday 15 March 2020

TestNG: Run test cases using maven

Using 'maven-surefire-plugin', we can run the testng test cases.

Example
 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.21.0</version>
    <configuration>
     <!-- TestNG Suite XML files list for test execution -->
     <suiteXmlFiles>
      <suiteXmlFile>src/test/resources/demo.xml</suiteXmlFile>
     </suiteXmlFiles>
    </configuration>
   </plugin>
  </plugins>
 </build> 

Add above snippet to pom.xml file. When you ran ‘mvn clean install’, it runs the test cases mentioned in demo.xml file.

Find the following application.

Step 1: Create ‘SurefirePluginTest.java’.
package com.sample.app.tests;

import static org.testng.Assert.assertTrue;

import org.testng.annotations.Test;

public class SurefirePluginTest {

 @Test
 public void a() {
  assertTrue(true);
 }

 @Test
 public void b() {
  assertTrue(true);
 }

 @Test
 public void c() {
  assertTrue(true);
 }
}

Step 2: Create demo.xml file in src/test/resources.

demo.xml
<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
 <test name="Groups smoke test demo">

  <classes>
   <class name="com.sample.app.tests.SurefirePluginTest" />
  </classes>
 </test>
</suite>  

Step 3: Update pom.xml with surefire plugin.

pom.xml
<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>com.sample.app</groupId>
 <artifactId>testng</artifactId>
 <version>1</version>

 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.21.0</version>
    <configuration>
     <!-- TestNG Suite XML files list for test execution -->
     <suiteXmlFiles>
      <suiteXmlFile>src/test/resources/demo.xml</suiteXmlFile>
     </suiteXmlFiles>
    </configuration>
   </plugin>
  </plugins>
 </build>

 <dependencies>
  <!-- https://mvnrepository.com/artifact/org.testng/testng -->
  <dependency>
   <groupId>org.testng</groupId>
   <artifactId>testng</artifactId>
   <version>7.0.0</version>
   <scope>test</scope>
  </dependency>

 </dependencies>
</project>

Navigate to the directory where pom.xml is located and execute the command ‘mvn clean install’.

If you are using eclipse, you can even run the application from eclipse.

Right click on the project -> Run As -> Run Configurations…


Click on Main tab, in the goals text field, add the string clean install
Click on Run button. You will see below messages in console

[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ testng ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.259 s - in TestSuite
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0


Previous                                                    Next                                                    Home

No comments:

Post a Comment