TestNG 中测试的执行顺序 [英] Order of execution of tests in TestNG

查看:24
本文介绍了TestNG 中测试的执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在TestNG中自定义测试的执行顺序?

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

例如:

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. For one execution the output may be:

test1
test3
test2

如何按照编写的顺序执行测试?

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

推荐答案

这会起作用.

@Test(priority=1)
public void Test1() {

}

@Test(priority=2)
public void Test2() {

}

@Test(priority=3)
public void Test3() {

}

priority 鼓励执行顺序,但不保证前一个优先级已经完成.test3 可以在 test2 完成之前开始.如果需要保证,则声明一个依赖项.

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.

与声明依赖项的解决方案不同,即使一个测试失败,使用 priority 的测试也会执行.根据 文档.

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.

这篇关于TestNG 中测试的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆