gpt4 book ai didi

java - 如果 @AfterMethod 失败,TestNG 会跳过后续运行 @Test(inplicationCount = 3)

转载 作者:太空宇宙 更新时间:2023-11-04 09:10:26 25 4
gpt4 key购买 nike

使用 testNG 运行此代码:

package org.example;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Test1 {

@BeforeMethod(alwaysRun = true)
public void before() throws Exception {
System.out.println("BEFORE, thread ID=" + Thread.currentThread().getId());
}

@AfterMethod(alwaysRun = true)
public void after() throws Exception {
System.out.println("AFTER, thread ID=" + Thread.currentThread().getId());
throw new Exception("Error!");
}

@Test(alwaysRun = true, invocationCount = 3, threadPoolSize = 1)
public void test() throws Exception {
System.out.println("TEST, thread ID=" + Thread.currentThread().getId());
}

}

返回

BEFORE, thread ID=1
TEST, thread ID=1
AFTER, thread ID=1
BEFORE, thread ID=1
AFTER, thread ID=1
BEFORE, thread ID=1
AFTER, thread ID=1

@Test 方法仅运行第一次,之后被跳过。如何实现这一点,即避免跳过 @Test 方法:

BEFORE, thread ID=1
TEST, thread ID=1
AFTER, thread ID=1
BEFORE, thread ID=1
TEST, thread ID=1
AFTER, thread ID=1
BEFORE, thread ID=1
TEST, thread ID=1
AFTER, thread ID=1

最佳答案

结果表明测试是按顺序运行的,而不是并行运行的。因此,单个线程用于运行测试用例的所有 3 次调用。

如果调用需要并行运行,则必须在 testng.xml 文件中设置以下参数:

  1. 并行=“方法”
  2. 线程数=“3”

用于并行运行的工作示例 testng.xml 文件:

<suite name="Run tests in parallel" parallel="methods" thread-count="3">
<test name="3-threads-in-parallel">
<classes>
<class name="com.company.tests.TestClass"/>
</classes>
</test>
</suite>

关于java - 如果 @AfterMethod 失败,TestNG 会跳过后续运行 @Test(inplicationCount = 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59723045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com