【问题标题】:How to call instances of input arguments of TestNg @Test method, after test execution using TestNg ITestResult interface or any other testNg interface在使用 TestNg ITestResult 接口或任何其他 testNg 接口执行测试后,如何调用 TestNg @Test 方法的输入参数的实例
【发布时间】:2019-03-25 18:11:14
【问题描述】:

我创建了一个实现 IReporter 接口的自定义报告器,并希望将测试输入参数发布到最终测试报告中。我的测试输入参数是通过 TestNg Dataprovider 提供的。每个输入参数都是 TestCase 类的一个实例。

我可以获取报告中的输入参数,但它只是对象的哈希码,而不是实例本身,我可以从中调用所需的测试数据并在 html 报告中发布。

我能够使用以下代码打印所有输入参数

Set<ITestResult> failedTests = testContext
                    .getFailedTests()
                    .getAllResults();
            for (ITestResult result: failedTests) {
                for (Object param: result.getParameters()) {
                    System.out.println(param);
                }
            }
Output:
data.service.entities.TestCase@1a1da881
org.testng.TestRunner@4dbb42b7

getParameters() 方法返回一个对象数组,我不知道如何转换为 TestCase。

请提供一种获取 data.service.entities.TestCase@1a1da881 实例的方法 为了调用它的方法。

【问题讨论】:

    标签: java testng testng-dataprovider


    【解决方案1】:

    为了获取实例,我创建了 TestNg CustomListener 类,我在其中手动将输入参数 (TestCase) 设置为每个已执行测试 ITestResult 的属性:

        public class CustomListener extends TestListenerAdapter {
    
        @Override
        public void onTestFailure(ITestResult iTestResult) {
            super.onTestFailure(iTestResult);
            TestCase tCase = (TestCase) iTestResult.getParameters()[0];
            iTestResult.setAttribute("failed_case", tCase);
        }
    
        @Override
        public void onTestSuccess(ITestResult iTestResult) {
            super.onTestSuccess(iTestResult);
            TestCase tCase = (TestCase) iTestResult.getParameters()[0];
            iTestResult.setAttribute("passed_case", tCase);
        }
    
        @Override
        public void onTestSkipped(ITestResult iTestResult) {
            super.onTestSkipped(iTestResult);
            TestCase tCase = (TestCase) iTestResult.getParameters()[0];
            iTestResult.setAttribute("skipped_case", tCase);
        }
      }
    

    在我的自定义报告类中,我得到了每个测试用例的对象,如下所示:

            TestCase failedCase = (TestCase) testResult.getAttribute("failed_case");
            TestCase passedCase = (TestCase) testResult.getAttribute("passed_case");
            TestCase skippedCase = (TestCase) testResult.getAttribute("skipped_case");
    

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 2016-03-04
      • 2018-10-06
      相关资源
      最近更新 更多