Created
January 6, 2012 22:50
-
-
Save randomsync/1572820 to your computer and use it in GitHub Desktop.
Suite dependency/ordering in TestNG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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