tags:

views:

410

answers:

5

How to customize the order of execution of tests in testng ?

For example:

@Test
public class Test1 {
  @Test
  public void test1() {
      System.out.println("test1");
  }

  @Test
  public void test2() {
      System.out.println("test2");
  }

  @Test
  public void test3() {
      System.out.println("test3");
  }
}

In the above suite, the order of execution of tests is arbitrary. i.e., sthe output is: test1 test3 test2

How do I execute the tests in the order in which they've been written?

A: 

Tests like unit tests? What for? Tests HAVE to be independant, otherwise.... you can not run a test individually. If they are independent, why even interfere? Plus - what is an "order" if you run them in multiple threads on multiple cores?

TomTom
It's actually quite possible to mix dependencies and parallelism, take a look at this article to find out how TestNG does it:http://beust.com/weblog/2009/11/28/hard-core-multicore-with-testng/
Cedric Beust
+1  A: 

There are ways of executing tests in a given order. Normally though, tests have to be repeatable and independent to guarantee it is testing only the desired fuctionality and is not dependent on side effects of code outside of what is being tested.

So, to answer your question, you'll need to provide more information such as WHY it is important to run tests in a specific order.

Kelly French
There are many situations where dependencies are useful, especially for integration and functional testing.For example, testing a web site: you want to test the login page first, and then the next page, etc... Clearing and recreating the state from scratch all the time is impractical and leads to very slow tests.Also, dependencies give you much better diagnostics, such as "1 test failed, 99 tests skipped" instead of the traditional "100 tests failed" which doesn't help realize that all these failures are actually because one test failed.
Cedric Beust
+2  A: 

In TestNG, you use dependsOnMethods and/or dependsOnGroups:

@Test(groups = "a")
public void f1() {}

@Test(groups = "a")
public void f2() {}

@Test(dependsOnGroups = "a")
public void g() {}

In this case, g() will only run after f1() and f2() have completed and succeeded.

You will find a lot of examples in the documentation: http://testng.org

Cedric Beust
+1  A: 

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

Kartik
+1  A: 

However, Cedric's solution has some drawbacks when working with the TestNG Eclipse Plugin, ver. 5.9.0.4 as it befeore every run of the TestCase shows message that groups are not suppoerted by this plugin.