0

I am using latest version of TestNG but still not able execute testcases by the order it has been written(avoiding priority annotation tag).

enter image description here

import org.testng.annotations.Test;
public class NewTest {
@Test
public void b() {
    System.out.println("inside b method");
}
@Test
public void a() {
System.out.println("inside a method");
}
}

I have also used IMethodInterceptor but still no go.

in testng.xml also added listeners:

<listeners>
<listener class-name="testngdemo.PriorityInterceptor" />
</listeners>

but still getting following output

inside a method
inside b method

Priority interface:

import java.lang.annotation.Retention; 
import java.lang.annotation.Target; 
import static java.lang.annotation.ElementType.METHOD; 
import static java.lang.annotation.ElementType.TYPE; 

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME) 
@Target({METHOD, TYPE}) 
public @interface Priority { int value() default 0; }
ChrisB
  • 2,497
  • 2
  • 24
  • 43
Javed Khan
  • 31
  • 10
  • http://stackoverflow.com/questions/2669576/order-of-execution-of-tests-in-testng what's wrong with this approach? – Imesha Sudasingha Oct 18 '16 at 10:04
  • @ImeshaSudasingha I followed the same procedure mentioned by jacobcs, but still it is executing in alphabetical order – Javed Khan Oct 18 '16 at 10:15
  • @ImeshaSudasingha I do get method id but it is not getting executed. Priority is [MethodInstance method=NewTest.b()[pri:0, instance:testngdemo.NewTest@6a024a67] instance=testngdemo.NewTest@6a024a67] method: b result: 0 – Javed Khan Oct 18 '16 at 10:19

2 Answers2

1

If you run your test cases from testng xml then include your test methods in the order you want like this:

<classes>

     ....
       ....
        <class name="Fully qualified class name without extension">
          <methods>
            <include name="method_1" />
            <include name="method_1" />
            .....
            .....
            <include name="method_N" />
          </methods>
        </class>

        <class name="test.Test2" />
     ....
    ....

</classes>
optimistic_creeper
  • 2,739
  • 3
  • 23
  • 37
0

What you are trying is not really clear but if you have a dependency between a and b, just use the dependsOnMethods feature:

public class NewTest {
  @Test
  public void b() {
    System.out.println("inside b method");
  }

  @Test(dependsOnMethods = { "b" })
  public void a() {
    System.out.println("inside a method");
  }
}
juherr
  • 5,640
  • 1
  • 21
  • 63
  • I do not have any dependency. As far I know testng execute methods in alphabetical order(Please correct me if I am wrong). – Javed Khan Oct 18 '16 at 10:12
  • I want to execute my testcases as per the order it has been written. Without using testng priority annotation tag. – Javed Khan Oct 18 '16 at 10:13
  • How do you run your application? Using testng xml or using any eclipse/intellij testng plugin? If you are not using any plugin then add test methods in the order as provided in my previous answer. – optimistic_creeper Oct 18 '16 at 11:43
  • I am running my test cases using testng.xml – Javed Khan Oct 18 '16 at 11:50
  • @JavedKhan As I remember, the default order depends on the Java reflection API. So the result may change depending on the jvm you use. Test methods are not supposed to be dependent and you have to use dependsOn features when they are. – juherr Oct 18 '16 at 13:01