执行 TestNG 测试时捕获 Log4j 输出

发布于 2024-09-09 10:58:08 字数 321 浏览 5 评论 0原文

我正在执行 TestNG 测试,并且日志输出设置为 DEBUG,因此如果发生故障,我可以准确检查出了什么问题。

问题是输出非常冗长,运行时会困扰每个人。我想捕获所有 Log4J 日志记录事件 - 这很简单 - 并且仅在测试失败时打印它们。另外,我需要考虑 @Before/@After 方法并打印它们的输出。

假设我已经有 Log4J LoggingEvent 列表,如何仅在 Test/After/Before< 时打印这些列表/code> 方法失败?

I am executing TestNG tests , and the logging output is set to DEBUG, so in case of a failure I can get to inspect exactly what goes wrong.

The problem is that the output is extremely verbose, and it's bothering everyone when it runs . I would like to capture all Log4J logging events - which is easy - and only print them when the test fails. Also, I need to take into account @Before/@After methods and also print output for them.

Assuming that I already have a List of Log4J LoggingEvents , how can I print those only when the Test/After/Before methods fail?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

摘星┃星的人 2024-09-16 10:58:08

使用 Reporter.log(str) 将消息记录在报告上。

@AfterMethod
public void printLOGonFailure(ITestResult result) {
    if (result.getStatus() == ITestResult.FAILURE) {
        String  str = getLog();
        Reporter.log(str);
    }
}

Use Reporter.log(str) to log the message on the report.

@AfterMethod
public void printLOGonFailure(ITestResult result) {
    if (result.getStatus() == ITestResult.FAILURE) {
        String  str = getLog();
        Reporter.log(str);
    }
}
寒江雪… 2024-09-16 10:58:08

这个网站有一个解释关于如何做到这一点。。我在这里复制了代码部分,以防链接失效。

Logger.getLogger(this.getClass())
log4j.rootLogger=ERROR,TESTAPPENDER
log4j.appender.TESTAPPENDER=com.my.fantastic.MockedAppender
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class FooTest {
    private Appender appenderMock;
    @Before
    public void setupAppender() {
        appenderMock = mock(Appender.class);
        Logger.getRootLogger().addAppender(appenderMock);
    }
    @After
    public void removeAppender() {
        Logger.getRootLogger().removeAppender(appenderMock);
    }
    @Test
    public void testMethod()  {
        doStuffThatCausesLogging();
        verify(appenderMock).doAppend((LoggingEvent) anyObject());
    }
}

ArgumentCaptor arguments = ArgumentCaptor.forClass(LoggingEvent.class);
verify(appenderMock).doAppend(arguments.capture());

assertThat(arguments.getValue().getLevel(), is(Level.WARN));

This site has a explanation on how to do it.. I copied the code part here in case the link goes dead.

Logger.getLogger(this.getClass())
log4j.rootLogger=ERROR,TESTAPPENDER
log4j.appender.TESTAPPENDER=com.my.fantastic.MockedAppender
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class FooTest {
    private Appender appenderMock;
    @Before
    public void setupAppender() {
        appenderMock = mock(Appender.class);
        Logger.getRootLogger().addAppender(appenderMock);
    }
    @After
    public void removeAppender() {
        Logger.getRootLogger().removeAppender(appenderMock);
    }
    @Test
    public void testMethod()  {
        doStuffThatCausesLogging();
        verify(appenderMock).doAppend((LoggingEvent) anyObject());
    }
}

ArgumentCaptor arguments = ArgumentCaptor.forClass(LoggingEvent.class);
verify(appenderMock).doAppend(arguments.capture());

assertThat(arguments.getValue().getLevel(), is(Level.WARN));

面犯桃花 2024-09-16 10:58:08

实现并注册 org.testng。 ITestListener 并对回调方法做出反应。

Implement and register a org.testng.ITestListener and react on the callback methods.

无人问我粥可暖 2024-09-16 10:58:08

将 JMockit 放入依赖项中。有了它,日志测试就变得非常容易。

放入测试类:

     @Cascading
final static Logger logging = Logger.getLogger(<some your>.class);

放入测试:

    testedFunction(a, b, c);
    new Verifications() {{
        logging.error("The message that should be output");
        logging.info("Another message");
    }};

Put JMockit in dependencies. With it logging testing is very easy.

Put in test class:

     @Cascading
final static Logger logging = Logger.getLogger(<some your>.class);

Put in test:

    testedFunction(a, b, c);
    new Verifications() {{
        logging.error("The message that should be output");
        logging.info("Another message");
    }};
玻璃人 2024-09-16 10:58:08

您可以像下面的代码一样使用:

@AfterMethod
public void printLOGonFailure(ITestResult result) {
    if (result.getStatus() == ITestResult.FAILURE) {
        final Logger logger = Logger.getLogger(Create_Case_from_an_Account.class);
        PropertyConfigurator.configure("C:\\Users\\svcSelenium\\eclipse-workspace\\SeleniumWork\\classes\\log4j.properties");       
        logger.error("Opps!!Test Failed due to:"+result.getThrowable());
    }
}

You can use like the below code:

@AfterMethod
public void printLOGonFailure(ITestResult result) {
    if (result.getStatus() == ITestResult.FAILURE) {
        final Logger logger = Logger.getLogger(Create_Case_from_an_Account.class);
        PropertyConfigurator.configure("C:\\Users\\svcSelenium\\eclipse-workspace\\SeleniumWork\\classes\\log4j.properties");       
        logger.error("Opps!!Test Failed due to:"+result.getThrowable());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文