【问题标题】:how to execute statements only after junit reports are generated by TESTNG?仅在 TESTNG 生成 junit 报告后如何执行语句?
【发布时间】:2015-12-01 05:59:27
【问题描述】:

我正在将 testng 生成的 junit 报告转换为其他格式。

我为此编写了这段代码:

@AfterTest
public void execute()
{
    String junitReport = "TEST-"+this.getClass().getCanonicalName()+".xml";
    TestManagerLogger obj = new TestManagerLogger();
    obj.convertLog(junitReport);

}

但这不起作用,因为在执行此方法之前不会生成报告。 有没有什么方法可以只在生成报表后调用该方法?

我的测试用例:

@Test(dataProvider = "jobCount")
public void testJobCount(String Scenario, String URL,String methodType, String status) {
    URL = URL.replaceFirst("ip", ip);
    String logonToken=LogonUtility.logon();
    String result=  ResponseGenerator.response(URL, logonToken, methodType);
    List<HashMap> valuesFromExcel = StringSplitter.getKeyValuePairs(status);// Returns hashmap containing key values ex: failed =0 , total =3
    List<HashMap> valuesFromRest = new ArrayList<HashMap>();
    Document doc = StringSplitter.convertStringToDocument(result);
    javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList node,node1;
    try{
        node =(NodeList)xPath.evaluate("/feed/entry/content/attrs/attr[@name='status_type']", doc, XPathConstants.NODESET);
        node1 = (NodeList) xPath.evaluate("/feed/entry/content/attrs/attr[@name='count']", doc, XPathConstants.NODESET);
        HashMap<String,String> hm = new HashMap<String,String>();

        for(int i=0;i<node.getLength();i++)
        {
            hm.put(node.item(i).getTextContent(),node1.item(i).getTextContent() );
        }
        valuesFromRest.add(hm);
        if(valuesFromRest.equals(valuesFromExcel))
        {
            AssertJUnit.assertTrue(true);

        }
        else
        {
            AssertJUnit.assertTrue(false);
        }


    }catch(Exception e) {
        e.printStackTrace();
    }


}

预期的 XML 报告

<logfile>
<logrecord>
    <case>scenario</case>
    <etime>Execution time</etime>
</logrecord>
</logfile>

场景在测试用例中作为参数传递

【问题讨论】:

  • 您将不得不使用像 jenkins 这样的 CI 来实现这一点,因为它提供了执行诸如后期构建之类的操作的选项。对于 testng @AfterTest 也是执行的一部分,所以它永远不会理解你想要的方式。

标签: junit automated-tests testng


【解决方案1】:

你应该做的是实现你自己的记者:http://testng.org/doc/documentation-main.html#logging-reporters

public class TestManagerReporter implements IReporter {

  public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
    // print <logfile>
    for (ISuite suite : suites) {
      for (IInvokedMethod method : suite.getAllInvokedMethods()) {
        if (method.isTestMethod()) {
          ITestResult result = method.getTestResult();
          if (result.getStatus() == SUCCESS) {
            // print <logrecord>
            // print <case>
            // print result.getName()
            // print </case>
            // print <etime>
            // print result.getEndMillis() - result.getStartMillis() 
            // print </etime>
            // print </logrecord>
          }
        }
      }
    }
    // print </logfile>
  }
}

【讨论】:

  • 感谢您的建议。我查看了文档,但从那里无能为力。如果你能告诉我在哪里可以找到详细的例子,那真的很有帮助。
  • 这个想法是从 TestNG 模型对象生成您自己的格式,而不是使用中间格式 (junit)。如果您需要帮助,您应该分享预期的格式。
  • 我在问题中添加了预期的 xml 报告格式。
  • 好的,我添加了一个示例,说明您可以执行哪些操作来生成预期格式。还有很多其他方法可以做到这一点,只需检查 TestNG API。我让你完成xml生成部分。
  • 感谢您的示例,开始研究此问题。我只是想再问你一个问题。我使用eclipse运行我的测试类,没有配置testng.xml,这个类会在测试用例执行后自动调用吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多