【发布时间】:2020-01-13 19:34:04
【问题描述】:
使用 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
【问题讨论】: