【发布时间】: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