java - 如果 @AfterMethod 失败,TestNG 会跳过后续的 @Test(invocationCount = 3) 运行
问题描述
使用 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 文件中设置以下参数:
- 并行=“方法”
- 线程数=“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>
推荐阅读
- instagram - How to figure out an Instagram username from the user id
- python - 为什么 3d 数组的打印结果与 python 中相同的心理可视化结果不同?
- r - 如何更改已与 R 中的对象一起保存的绘图中某些点的颜色?
- thinkscript - thinkscript if 函数在重要情况下无用
- django - 如何通过一个
- c++ - 我们可以将 operator++(int) 重载为指针吗?
- python - 使用 Pandas 在 Python 中将输出显示为 CSV
- javascript - 如何在 JavaScript 中将字符串与连字符匹配?
- php - 一般错误:1364 字段“已发布”没有默认值
- ajax - ASP.NET Core 2.2 Razor 页面 - AJAX 调用页面模型不返回数据