Question

I have a java class containing 3 methods:

public class Test{
 public void orange(){
 }
 public void apple(){
 }
 public void mango(){
 }
}

I want to execute 3 methods mentioned above sequentially/orderly as i have written by Selenium RC and TestNG. How can I do this?

Was it helpful?

Solution

The easy way is to just change @Test to @Test(singleThreaded=true). If you do, all of the tests in your class will run sequentially in a single thread.

Or

If you want to be explicit about the order that the tests should run in, you can use the annotation @dependsOnMethods

public void orange(){}

@Test(dependsOnMethods = { "orange" })
public void apple(){}

@Test(dependsOnMethods = { "apple" })
public void mango(){}

This is also nice if you want some, but not all, of the methods in a class to run sequentially.

http://testng.org/doc/documentation-main.html#dependent-methods

OTHER TIPS

Just change the @Test to @Test(singleThreaded=true) and you're good to go.

http://testng.org/javadoc/org/testng/annotations/Test.html#singleThreaded%28%29

In your test class you try this annotation at class level itself.

@Test(sequential = true)

I suggest using dependsOnGroups. Hence you club your test method as one group and provide dependency over this group. So tomorrow if you refactor your method name, your dependency structure would not be broken. For more on dependsOnGroups look here

In addition to using sequential=true on the class, you can also set a priority on the methods themselves.

@Test(priority=1)
public void orange(){}

@Test(priority=2)
public void apple(){}

@Test(priority=3)
public void mango(){}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top