- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 2 个 TestNG 监听器,它们将日志记录信息报告到日志文件以获取调试信息。它们是 IConfigurationListener2
和 ITestListener
。测试在多个线程中运行。
我的问题是,我需要将 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
类是:
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
方法。我想要这样的日志:
开始于
启动测试方法: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());
}
}
关于java - TestNG:如何将 IConfigurationListener2.beforeConfiguration ITestResult 链接到 ITestListener.onTestStart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41855983/
目前我正在使用 ExtentReport 生成自动化报告。 我使用 ExtentReport 的方式是使用 IReporter 实现在测试结束时生成报告,这很棒。 但是,现在我正在考虑创建一种在执行测
我正在为我的 testng-selenium-java 项目使用 ExtentReports 和 ItestListener,我的监听器截取了 ExtentReports 失败测试用例的屏幕截图,但问
我需要生成 testng 范围报告(版本 3)。我看到有两种方法可以生成范围报告。 这里,我不使用代码来解释两种情况。 Case 1: Creating a class implementing "I
有人知道如果测试失败首先会调用什么吗? 最佳答案 如果使用注解@AfterMethod,则注解的方法将在每个测试方法之后运行。如果您使用alwaysRun选项,那么即使先前调用的一个或多个方法失败或被
非常感谢您在这里提供任何建议。我正在尝试使用 TestNG 并实现 ITestListerner 接口(interface)来使用 POM 框架中的驱动程序对象。 这是一个实现 ItestListen
我有 2 个 TestNG 监听器,它们将日志记录信息报告到日志文件以获取调试信息。它们是 IConfigurationListener2 和 ITestListener。测试在多个线程中运行。 我的
当我使用 testng.xml 执行测试时,它运行正常,但是当我使用 pom.xml 执行测试时,出现错误: [ERROR] java.lang.UnsupportedClassVersionErro
我是一名优秀的程序员,十分优秀!