How to customize the order of execution of tests in TestNG?
For example:
public class Test1 {
@Test
public void test1() {
System.out.printl
@Test(dependsOnMethods="someBloodyMethod")
If I understand your question correctly in that you want to run tests in a specified order, TestNG IMethodInterceptor can be used. Take a look at http://beust.com/weblog2/archives/000479.html on how to leverage them.
If you want run some preinitialization, take a look at IHookable http://testng.org/javadoc/org/testng/IHookable.html and associated thread http://groups.google.com/group/testng-users/browse_thread/thread/42596505990e8484/3923db2f127a9a9c?lnk=gst&q=IHookable#3923db2f127a9a9c
Piggy backing off of user1927494's answer, In case you want to run a single test before all others, you can do this:
@Test()
public void testOrderDoesntMatter_1() {
}
@Test(priority=-1)
public void testToRunFirst() {
}
@Test()
public void testOrderDoesntMatter_2() {
}
This will work.
@Test(priority=1)
public void Test1() {
}
@Test(priority=2)
public void Test2() {
}
@Test(priority=3)
public void Test3() {
}
priority
encourages execution order but does not guarantee the previous priority level has completed. test3
could start before test2
completes. If a guarantee is needed, then declare a dependency.
Unlike the solutions which declare dependencies, tests which use priority
will execute even if one test fails. This problem with dependencies can be worked around with @Test(...alwaysRun = true...)
according to documentation.
I've faced the same issue, the possible reason is due to parallel execution of testng and the solution is to add Priority option or simply update preserve-order="true" in your testng.xml.
<test name="Firefox Test" preserve-order="true">