We might face some scenarios where we have to make a testng.xml file at runtime or programmatically. So, in this post, we will learn how to make a testng.xml file at runtime and execute it.
Let’s first create a test case that we want to execute programmatically
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);
}
}
We will create a testng.xml file that will help us 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 CodekruTest class.
Output –
Excecuting the test
===============================================
codekru
Total tests run: 1, Failures: 0, Skips: 0
Now, how to create 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, and let’s try to make the equivalent of the above XML file, it will look something like this.
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();
So, the whole class would be
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();
}
}
Now, run this main() method like a standard java application and all test cases under Test.CodekruTest will also execute.
Output –
Excecuting the test
===============================================
codekru
Total tests run: 1, Failures: 0, Skips: 0
===============================================
There are many “What If” scenarios that are left –
- What if we wanted to include some of the methods from a class? How can we achieve that programmatically?
- What if we wanted to exclude some of the methods from a class?
- What if we wanted to change the output directory of the reports?
Don’t worry. We have covered it all, too. Give it a try in our next post.
We hope that you liked the article. If you have any doubts, please feel free to write us in the comments or mail us at [email protected].