Skip to content

Instantly share code, notes, and snippets.

@randomsync
Created January 6, 2012 22:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randomsync/1572820 to your computer and use it in GitHub Desktop.
Save randomsync/1572820 to your computer and use it in GitHub Desktop.
Suite dependency/ordering in TestNG
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();
}
}
}
import org.testng.ITestContext;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class SlowTest {
ITestContext context;
@BeforeMethod
public void setup(ITestContext context) {
this.context = context;
}
@Test
public void test2() {
System.out.println("Now Running: " + context.getSuite().getName()
+ ", SlowTest");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.util.ArrayList;
import java.util.List;
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.setSuiteThreadPoolSize(2);
// testng.setVerbose(2);
List<XmlSuite> suites = new ArrayList<XmlSuite>();
// create 1 fast and 1 slow suite
for (int i = 0; i < 2; i++) {
XmlSuite suite = new XmlSuite();
suite.setName("Suite " + i);
// add 2 tests
for (int j = 0; j < 2; j++) {
XmlTest xmltest = new XmlTest(suite);
List<XmlClass> classes = new ArrayList<XmlClass>();
if (i % 2 == 0)
classes.add(new XmlClass("FastTest"));
else
classes.add(new XmlClass("SlowTest"));
xmltest.setXmlClasses(classes);
}
suites.add(suite);
}
// create 2 slow suites
for (int i = 2; i < 4; i++) {
XmlSuite suite = new XmlSuite();
suite.setName("Suite " + i);
// add 2 tests
for (int j = 0; j < 2; j++) {
XmlTest xmltest = new XmlTest(suite);
List<XmlClass> classes = new ArrayList<XmlClass>();
if (i % 2 == 0)
classes.add(new XmlClass("FastTest"));
else
classes.add(new XmlClass("SlowTest"));
xmltest.setXmlClasses(classes);
}
suites.add(suite);
}
testng.setXmlSuites(suites);
testng.run();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
@randomsync
Copy link
Author

Output:

Now Running: Suite 0, FastTest
Now Running: Suite 1, SlowTest
Now Running: Suite 0, FastTest

Suite 0

Total tests run: 2, Failures: 0, Skips: 0

Now Running: Suite 2, FastTest
Now Running: Suite 2, FastTest

Suite 2

Total tests run: 2, Failures: 0, Skips: 0

Now Running: Suite 3, SlowTest
Now Running: Suite 1, SlowTest
Now Running: Suite 3, SlowTest

Suite 1

Total tests run: 2, Failures: 0, Skips: 0

Suite 3

Total tests run: 2, Failures: 0, Skips: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment