In this post, we will create the below testng.xml file at runtime using Java code.
<suite name="codekru">
<test name="codekru">
<classes>
<class name="Test.CodekruTest" />
</classes>
</test>
</suite>
Creating a testng.xml file may be necessary in various situations. For example, when running TestNG tests from Jenkins, we can pass specific parameters from Jenkins to our code and then generate a testng.xml file based on the parameters received from Jenkins.
Note: We have also attached a video at the end of this article. In case you are more comfortable with the video version, then please feel free to have a look
Create a test case
Let’s create a test case, which we will execute by using the testng.xml created at runtime by the code.
package Test;
import org.testng.Assert;
import org.testng.annotations.Test;
public class CodekruTest {
@Test
public void executeTest() {
System.out.println("Excecuting the test");
Assert.assertTrue(true);
}
}
Now, let’s create an XML file to run the above test case
<suite name="codekru">
<test name="codekru">
<classes>
<class name="Test.CodekruTest" />
</classes>
</test>
</suite>
The above XML file will execute every test case under the CodekruTest class.
Output –
Excecuting the test
===============================================
codekru
Total tests run: 1, Failures: 0, Skips: 0
Creating the above XML file programmatically
We have equivalents of the <suite>, <test>, <class> tags in org.testng package.
- <suite> is equivalent to XmlSuite class.
- <test> is equivalent to XmlTest class.
- <class> is equivalent to XmlClass class.
So, keeping that in mind. The below code is the equivalent of the above XML file.
XmlSuite suite = new XmlSuite();
suite.setName("codekru"); // this means <suite name = "codekru">
XmlTest test = new XmlTest(suite);
test.setName("codekru"); // this means <test name = "codekru">
List<XmlClass> classes = new ArrayList<XmlClass>(); // <classes>
classes.add(new XmlClass("Test.CodekruTest")); // this means <class name = "Test.CodekruTest">
test.setXmlClasses(classes);
To execute the suite named “codekru”, we have to make a TestNG object and pass a list of suites to it, as shown below.
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG testng = new TestNG();
testng.setXmlSuites(suites);
testng.run();
Below code will automatically run the test cases of the CodekruTest class, eliminating the need to save any testng.xml files.
import java.util.ArrayList;
import java.util.List;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
public class GenerateXmlAndExecuteItAtRuntime {
public static void main(String[] args) {
XmlSuite suite = new XmlSuite();
suite.setName("codekru"); // this means <suite name = "codekru">
XmlTest test = new XmlTest(suite);
test.setName("codekru"); // this means <test name = "codekru">
List<XmlClass> classes = new ArrayList<XmlClass>(); // <classes>
classes.add(new XmlClass("Test.CodekruTest")); // this means <class name = "Test.CodekruTest">
test.setXmlClasses(classes);
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG testng = new TestNG();
testng.setXmlSuites(suites);
testng.run();
}
}
Output –
Excecuting the test
===============================================
codekru
Total tests run: 1, Failures: 0, Skips: 0
===============================================
There are many “what if” scenarios left, which we will cover in another post.
- What if we wanted to include only certain methods from a class?
- What if we wanted to exclude a few methods from a class?
- What if we wanted to change the output directory of the reports?
You can also watch the video below for a step-by-step walkthrough of creating an XML file at runtime.
Video Tutorial
We hope that you liked the article. If you have any questions, please feel free to write to us in the comments or email us at admin@codekru.com.