Testng:如何在testng监听器中获取断言信息?

Testng: how to get assertion infomation in testng listner?

这是我的测试用例:

public class Demo2  {
    @Test
    public void dd() {
        assertThat("a").isEqualTo("b");
    }
}

还有一个 class extends TestListenerAdapter:

public class DemoListner extends TestListenerAdapter {
    
    @Override
    public void onTestFailure(ITestResult tr) {
        super.onTestFailure(tr);
        // how could get the assertion fail  message  ("a is not equal to b")in here ?
    }
}

在方法 onTestFailure 中是否有获取断言失败消息的方法?

异常存储在 ITestResult 本身。您可以使用以下方式获取详细信息:

Throwable t = tr.getThrowable();
// now you can use this throwable object to get the details..

//example
System.out.println(t.getMessage());