Testng分别获取并行执行方法日志 [英] Testng get parallel execution method logs separately

查看:127
本文介绍了Testng分别获取并行执行方法日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用onTestSuccess和OnTestFailure来测试用例执行结果.我能够报告我的测试用例是通过还是失败.

I have used onTestSuccess, OnTestFailure for Test case execution results. I am able to report my Test case is pass or fail.

if (result.getStatus() == ITestResult.SUCCESS) {
//
} else if (result.getStatus() == ITestResult.FAILURE) {
//
}else if (result.getStatus() == ITestResult.SKIP) {
//
}

但是我还需要分别捕获每种方法的日志..不应与其他线程日志重叠.

But I also need to capture logs for each method separately.. that should not overlapping with other thread logs.

@Test
public void test001() throws IOException {
    System.out.println("Test001");
}

@Test
public void test002() throws IOException {
    System.out.println("Test001");
}

有人可以提供帮助或建议吗?

Can someone help or suggestions ?

推荐答案

是的,您可以很容易地做到这一点.但是唯一的警告是您需要访问测试方法( @Test 方法)的 ITestResult 对象以获取其日志.您要做的就是使用 Reporter.log()记录消息,然后使用 Reporter.getOutput()检索日志.

Yes you can do it very easily. But the only caveat is that you would need access to the ITestResult object of a test method (@Test method) to get hold of its logs. All you need to do is, use Reporter.log() to log messages and then use Reporter.getOutput() to retrieve the logs.

这是一个示例,展示了这一点.

Here's a sample that shows this in action.

这是测试类的样子.

import org.testng.Reporter;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(TestCaseLogPrinter.class)
public class TestClassSample {
    @Test
    public void test001() {
        Reporter.log("Test001 : This is first message", true);
        Reporter.log("Test001 : This is second message", true);
    }

    @Test
    public void test002() {
        Reporter.log("Test002 : This is a random message", true);
        Reporter.log("Test002 : This is another random message", true);
    }
} 

这是一个检索日志的侦听器

Here's a listener that retrieves the logs

import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;

import java.util.List;

public class TestCaseLogPrinter extends TestListenerAdapter {
    @Override
    public void onTestSuccess(ITestResult tr) {
        System.err.println("Printing the test method logs " + asString(Reporter.getOutput(tr)));
    }

    private String asString(List<String> output) {
        StringBuilder builder = new StringBuilder();
        for (String each : output) {
            builder.append(each).append(", ");
        }
        //Removing the last ","
        return builder.toString().substring(0, builder.length() - 2);
    }
}

这是套件xml文件的样子:

Here's how the suite xml file looks like :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="49493003_Suite" parallel="methods" verbose="2">
    <test name="49493003_test" verbose="2">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn49493003.TestClassSample"/>
        </classes>
    </test>
</suite>

这是控制台输出

... TestNG 6.14.3 by Cédric Beust (cedric@beust.com)
...
Test001 : This is first message
Test002 : This is a random message
Test001 : This is second message
Test002 : This is another random message
Printing the test method logs Test001 : This is first message, Test001 : This is second message
Printing the test method logs Test002 : This is a random message, Test002 : This is another random message
PASSED: test001
PASSED: test002

===============================================
    49493003_test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

===============================================
49493003_Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

这篇关于Testng分别获取并行执行方法日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆