TestNG:如何 link IConfigurationListener2.beforeConfiguration ITestResult 到 ITestListener.onTestStart
TestNG: How to link IConfigurationListener2.beforeConfiguration ITestResult to ITestListener.onTestStart
我有 2 个 TestNG 侦听器,它们将日志记录信息报告到日志文件以获取调试信息。它们是 IConfigurationListener2
和 ITestListener
。多线程测试运行。
我的问题是我需要 link IConfigurationListener2.onConfigurationFailure()
方法中的 ITestResult 到 ITestListener.onTestStart()
ITestResult
来检索 @Test
ITestResult.getMethodName()
.这可能吗?
TestListener
代码为:
public class TestListener implements ITestListener{
@Override
public void onTestStart(ITestResult result) {
System.out.println("Starting test method:" + result.getMethod().getMethodName());
}
}
IConfigurationListener2
是:
public class ConfigurationListener implements IConfigurationListener2 {
@Override
public void onConfigurationFailure(ITestResult result) {
System.out.println("Failed to run " + result.getMethod().getMethodName()
+ " for the test method:" + <code needed>);
}
TestNG
Class是:
public class Script1 {
private int i =0;
@BeforeMethod
public void before(){
System.out.println("Starting before");
i++;
if (i==1){
throw new RuntimeException("forcing an exception");
}
}
@Test(testName="script1")
public void script1_run(){
System.out.println("Running script");
}
@Test(testName="script2")
public void script2_run(){
System.out.println("Running script");
}
}
那么我如何找出 @beforeMethod
失败的 @Test
方法。我想要这样的日志:
开始于
开始测试 method:script1_run
运行 脚本
开始于
之前运行测试方法失败:script2_run
谢谢,
您需要合并您的 2 个监听器:
public class MyNewListener implements IConfigurationListener2, ITestListener {
private final ThreadLocal<ITestResult> currentTest = new ThreadLocal<>();
@Override
public void onTestStart(ITestResult result) {
System.out.println("Starting test method:" + result.getMethod().getMethodName());
currentTest.set(result);
}
@Override
public void onConfigurationFailure(ITestResult result) {
System.out.println("Failed to run " + result.getMethod().getMethodName()
+ " for the test method:" + currentTest.get().getMethod().getMethodName());
}
}
ThreadLocal
用于跟踪平行运行。
免责声明:我没有在现实生活中测试听众。告诉我它是否有效。
编辑:配置方法失败后应该跳过测试
public class MyNewListener implements IConfigurationListener2, ITestListener {
private final ThreadLocal<ITestResult> failedConfiguration = new ThreadLocal<>();
@Override
public void onTestStart(ITestResult result) {
System.out.println("Starting test method:" + result.getMethod().getMethodName());
}
@Override
public void onConfigurationFailure(ITestResult result) {
failedConfiguration.set(result);
}
@Override
public void onTestSkipped(ITestResult result) {
System.out.println("Failed to run " + failedConfiguration.get().getMethod().getMethodName()
+ " for the test method:" + result.getMethod().getMethodName());
}
}
我有 2 个 TestNG 侦听器,它们将日志记录信息报告到日志文件以获取调试信息。它们是 IConfigurationListener2
和 ITestListener
。多线程测试运行。
我的问题是我需要 link IConfigurationListener2.onConfigurationFailure()
方法中的 ITestResult 到 ITestListener.onTestStart()
ITestResult
来检索 @Test
ITestResult.getMethodName()
.这可能吗?
TestListener
代码为:
public class TestListener implements ITestListener{
@Override
public void onTestStart(ITestResult result) {
System.out.println("Starting test method:" + result.getMethod().getMethodName());
}
}
IConfigurationListener2
是:
public class ConfigurationListener implements IConfigurationListener2 {
@Override
public void onConfigurationFailure(ITestResult result) {
System.out.println("Failed to run " + result.getMethod().getMethodName()
+ " for the test method:" + <code needed>);
}
TestNG
Class是:
public class Script1 {
private int i =0;
@BeforeMethod
public void before(){
System.out.println("Starting before");
i++;
if (i==1){
throw new RuntimeException("forcing an exception");
}
}
@Test(testName="script1")
public void script1_run(){
System.out.println("Running script");
}
@Test(testName="script2")
public void script2_run(){
System.out.println("Running script");
}
}
那么我如何找出 @beforeMethod
失败的 @Test
方法。我想要这样的日志:
开始于
开始测试 method:script1_run
运行 脚本
开始于
之前运行测试方法失败:script2_run
谢谢,
您需要合并您的 2 个监听器:
public class MyNewListener implements IConfigurationListener2, ITestListener {
private final ThreadLocal<ITestResult> currentTest = new ThreadLocal<>();
@Override
public void onTestStart(ITestResult result) {
System.out.println("Starting test method:" + result.getMethod().getMethodName());
currentTest.set(result);
}
@Override
public void onConfigurationFailure(ITestResult result) {
System.out.println("Failed to run " + result.getMethod().getMethodName()
+ " for the test method:" + currentTest.get().getMethod().getMethodName());
}
}
ThreadLocal
用于跟踪平行运行。
免责声明:我没有在现实生活中测试听众。告诉我它是否有效。
编辑:配置方法失败后应该跳过测试
public class MyNewListener implements IConfigurationListener2, ITestListener {
private final ThreadLocal<ITestResult> failedConfiguration = new ThreadLocal<>();
@Override
public void onTestStart(ITestResult result) {
System.out.println("Starting test method:" + result.getMethod().getMethodName());
}
@Override
public void onConfigurationFailure(ITestResult result) {
failedConfiguration.set(result);
}
@Override
public void onTestSkipped(ITestResult result) {
System.out.println("Failed to run " + failedConfiguration.get().getMethod().getMethodName()
+ " for the test method:" + result.getMethod().getMethodName());
}
}