TestNG之执行顺序

如果很有个测试方法,并且这几个方法又有先后顺序,那么如果让TestNG按照自己想要的方法执行呢

一、通过Dependencies

1.在测试类中添加Dependencies

复制代码
    @Test
    public void test1() {
        System.out.println("this is test1");
    }
    @Test(dependsOnMethods = { "test1" })
    public void test2() {
        System.out.println("this is test2");
    }
    @Test(dependsOnMethods = { "test2" })
    public void test3() {
        System.out.println("this is test3");
    }
    @Test(dependsOnMethods = { "test3" })
    public void test4() {
        System.out.println("this is test4");
    }
    @Test(dependsOnMethods = { "test4" })
    public void test5() {
        System.out.println("this is test5");
    }
复制代码

执行顺序,print:

this is test1
this is test2
this is test3
this is test4
this is test5

2.在xml文件中添加Dependencies,代码中无需再添加Dependencies

复制代码
  <test name="Test">
    <classes>
      <class name="test.testng.TestOrder"/>
      <methods>
      <dependencies>
          <method name="test2" depends-on="test1"/>
          <method name="test3" depends-on="test2"/>
          <method name="test4" depends-on="test3"/>
          <method name="test5" depends-on="test4"/>
          <method name="test1" />
          </dependencies>
      </methods>
    </classes>
  </test>
复制代码

执行顺序,打印结果:

this is test1
this is test2
this is test3
this is test4
this is test5

二、通过preserve-order="true"

在xml添加<suite name="Suite" preserve-order="true">,方法将按照XML中配置的先后顺序由上按下执行(代码无需额外控制)

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" preserve-order="true">
  <test name="Test">
    <classes>
      <class name="test.testng.TestOrder"/>
    <methods>
    <include name="test2" />
    <include name="test3" />
    <include name="test1" />
    </methods>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
复制代码

执行顺序,打印结果:

this is test2
this is test3
this is test1

三、并发执行,Parallel

1、如果没有并发执行用例的需要,建议把Parallel=false,如果等于Parallel=true,则会并发执行用例,除非你设置了Dependencies,否则执行的顺序将不受控制

如:<suite name="Suite" parallel="true">

2、添加Parallel的时候也可以设置线程数,如:

<suite name="My suite" parallel="methods" thread-count="5">

<suite name="My suite" parallel="tests" thread-count="5">

<suite name="My suite" parallel="classes" thread-count="5">

<suite name="My suite" parallel="instances" thread-count="5">

官方的解释,有兴趣的可以自行翻译:

  parallel="methods": TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.

  parallel="tests": TestNG will run all the methods in the same <test> tag in the same thread, but each <test> tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same         <test> and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.

  parallel="classes": TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.

  parallel="instances": TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.

Additionally, the attribute thread-count allows you to specify how many threads should be allocated for this execution.

posted on   乔叶叶  阅读(2398)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示