TestNG中的执行顺序,不使用优先级

时间:2016-10-18 09:53:13

标签: java testing testng

我使用的是最新版本的TestNG,但仍然无法按照编写的顺序执行测试用例(避免使用优先级注释标记)。

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");
}
}

我也使用了IMethodInterceptor,但仍然没有。

testng.xml中的

还添加了监听器:

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

但仍然得到以下输出

inside a method
inside b method

优先级界面:

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; }

2 个答案:

答案 0 :(得分:1)

如果您从testng xml运行测试用例,那么按照您想要的顺序包含您的测试方法:

<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>

答案 1 :(得分:0)

您尝试的内容并不是很清楚,但如果您在ab之间存在依赖关系,请使用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");
  }
}
相关问题