How to Create testng.xml dynamically?

43 views
Skip to first unread message

raj priya

unread,
Sep 28, 2022, 10:57:53 AM9/28/22
to testng-users
Hi,

Can anyone help me with the below request. 

I want testng.xml file to be created dynamically based on the input given in the Excel sheet for eg: if there are 5-10 test scenarios whose flag is set to Yes to execute, then i need to add those test scenarios in testng.xml file to execute and skip the test scenarios to execute when flag is set to No.


Thanks,
Priya Raj



renyiw...@gmail.com

unread,
Sep 28, 2022, 1:06:52 PM9/28/22
to testng-users
i think you can use excel to run testcase, Skip creat the xml





---- Replied Message ----
From raj priya<raj.rp...@gmail.com>
Date 09/28/2022 22:57
To testng-users<testng...@googlegroups.com>
Cc
Subject [testng-users] How to Create testng.xml dynamically?
--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/66f1f314-834c-4340-9337-bf6fd08d5be1n%40googlegroups.com.

Siva Nagaraju

unread,
Sep 28, 2022, 4:28:57 PM9/28/22
to testng...@googlegroups.com
We can do by using Java program to create dynamic testing.xml

Kalyan Kumar

unread,
Sep 29, 2022, 2:10:13 AM9/29/22
to testng...@googlegroups.com
You can create testng.xml file programmatically depend on excel

⇜Krishnan Mahadevan⇝

unread,
Oct 2, 2022, 5:27:04 AM10/2/22
to testng...@googlegroups.com
Priya Raj,

The below documented sample should explain how to do this on your own (I am using latest released version of TestNG v7.6.1 which needs JDK11)

Java
package com.rationaleemotions.runtime;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class TestRunner {

  public static void main(String[] args) {
    List<String> classes = getClassesToRun();
    if (classes.isEmpty()) {
      throw new IllegalStateException("There were no test classes that were found to be executed");
    }

    XmlSuite xmlSuite = new XmlSuite();
    xmlSuite.setVerbose(2);
    xmlSuite.setName("My_Test_Suite"); //This is how you set the Test Suite Name.

    List<XmlClass> xmlClasses = classes.stream()
        .map(XmlClass::new)
        .collect(Collectors.toList());

    XmlTest xmlTest = new XmlTest(xmlSuite);
    xmlTest.setXmlClasses(xmlClasses);

    TestNG testng = new TestNG();
    testng.setXmlSuites(Collections.singletonList(xmlSuite));

    //Since we are using the TestNG APIs we dont need to create a suite file.
    //We can directly execute the tests via the TestNG api.

    testng.run(); // This executes your tests.

    int exitCode = testng.getStatus();
    if (exitCode != 0) {
      throw new IllegalStateException("Not all tests ran successfully");
    }

  }

  private static List<String> getClassesToRun() {
    //Include the following logic here
    //1. Read from Excel
    //2. Filter out ONLY those rows that have the "execute flag" column set to "TRUE"
    //3. Return the list of fully qualified test class names as a list.
    return Arrays.asList(
        "com.rationaleemotions.runtime.TestClassOne",
        "com.rationaleemotions.runtime.TestClassTwo"
    );
  }

}



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


On Wed, Sep 28, 2022 at 8:41 PM raj priya <raj.rp...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages