TestNG 中测试的执行顺序

编程入门 行业动态 更新时间:2024-10-27 10:30:09
本文介绍了TestNG 中测试的执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

如何在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 的测试也会执行.根据 @Test(...alwaysRun = true...) 解决这个依赖问题.html#annotations">文档.

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

更多推荐

[db:关键词]

本文发布于:2023-04-30 23:04:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1401098.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:顺序   测试   TestNG

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!