Skip to content

Instantly share code, notes, and snippets.

@randomsync
Created January 17, 2012 22:16
Show Gist options
  • Save randomsync/1629341 to your computer and use it in GitHub Desktop.
Save randomsync/1629341 to your computer and use it in GitHub Desktop.
TestNG bug: When running TestNG programmatically, child xml suites are not run (when added using setSuiteFIles())
import org.testng.ITestContext;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class FastTest {
ITestContext context;
@BeforeMethod
public void setup(ITestContext context) {
this.context = context;
}
@Test
public void test1() {
System.out.println("Now Running: " + context.getSuite().getName()
+ ", FastTest");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite 1">
<test name="FastTest1" preserve-order="false">
<classes>
<class name="FastTest"/>
</classes>
</test>
</suite>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite verbose="2" name="TestNG XML File Suite">
<suite-files>
<suite-file path="./Suite1.xml"/>
</suite-files>
<test name="Test 1" preserve-order="true">
<classes>
<class name="FastTest"/>
</classes>
</test> <!-- Test 1 -->
</suite> <!-- Programmatic XmlSuite -->
import java.util.Arrays;
import org.testng.TestNG;
import org.testng.xml.*;
public class TestRunner {
static TestNG testng;
public static void main(String[] args) {
try {
testng = new TestNG();
testng.setPreserveOrder(true);
testng.setVerbose(2);
// create a suite programmatically
XmlSuite suite = new XmlSuite();
suite.setName("Programmatic XmlSuite");
// create a test case for the suite
XmlTest xmltest = new XmlTest(suite);
xmltest.setName("Test 1");
xmltest.setXmlClasses(Arrays.asList(new XmlClass("FastTest")));
// add a suite-file to the suite
suite.setSuiteFiles(Arrays.asList("./Suite1.xml"));
// 1. To run with testng.xml file, uncomment this one, comment 2
// testng.setTestSuites(Arrays.asList("testng.xml"));
// 2. to run with XmlSuite, uncomment this one, comment 1
testng.setXmlSuites(Arrays.asList(suite));
testng.run();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment