【问题标题】:How to pass values from test method to TestNG report如何将值从测试方法传递到 TestNG 报告
【发布时间】:2016-10-21 11:19:28
【问题描述】:

作为 Selenium 自动化框架的一部分,我需要编写一个方法来生成自定义 TestNG 报告。我知道这可以通过覆盖来实现

public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) 

IReporter 接口中的方法。但问题是我的测试方法计算了一些值,我必须将这些值传递给 testNG 报告。如何在 testNG 报告中打印测试方法的值?

【问题讨论】:

    标签: java testng


    【解决方案1】:

    一个ITestResult 对象(可以通过调用Reporter.getCurrentTestResult()@Test 方法中访问此对象)基本上有一个setAttribute 方法,它接受一个字符串键,其值为Object 对象。

    因此,您可以简单地在 @Test 方法中使用类似下面的内容,将测试计算的值保存到相应的 ITestResult 对象中,然后从 IReporter 实现中检索它。

    @Test
    public void myTestMethod() {
        Map<String, Object> computedItems = new HashMap<>();
        //Lets assume that the computedItems is what we need to save for retrieval from our reports.
        ITestResult testResult = Reporter.getCurrentTestResult();
        testResult.setAttribute("key", computedItems);
    }
    

    【讨论】:

      【解决方案2】:

      所有测试数据都存储在 ITestResult 中:

      for (ISuite suite : suites) {
          ...
          for (ISuiteResult result : suite.getResults().values())
              ...
              IResultMap iFailed = result.getTestContext().getFailedTests();
              for(ITestResult itr: iFailed.getAllResults()) {
                  ...
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-22
        • 2013-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多