【问题标题】:Testng get parallel execution method logs separatelytestng分别获取并行执行方法日志
【发布时间】:2018-03-26 13:43:58
【问题描述】:

我使用了 onTestSuccess、OnTestFailure 来获取测试用例的执行结果。我可以报告我的测试用例是通过还是失败。

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

但我还需要分别为每个方法捕获日志。不应该与其他线程日志重叠。

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

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

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

【问题讨论】:

标签: selenium-webdriver automation testng remotewebdriver


【解决方案1】:

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

这里有一个示例,展示了这一点。

这是测试类的样子。

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);
    }
} 

这是一个获取日志的监听器

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 文件如下所示:

<?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

【讨论】:

    【解决方案2】:

    您也可以通过AfterMethodTestNG 检索日志,同样明智:

    @AfterMethod
    public void calltestStatus(ITestResult result) throws IOException
    {
        testStatus(result);     
    }
    
    public void testStatus(ITestResult result) throws IOException
    {
        if (result.getStatus() == ITestResult.FAILURE) {
            System.out.println("");     
    
        } else if (result.getStatus() == ITestResult.SUCCESS) {
            System.out.println(""); 
    
        } else if (result.getStatus() == ITestResult.SKIP) {
            System.out.println(""); 
    
        } else {
            System.out.println(""); 
        }
    }
    

    其中 testStatus 是用户定义的函数,它处理测试方法的每个通过/失败状态,已在 AfterMethod 上调用,因此在完成每个测试后它将通过并返回状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多