如何仅在TESTNG生成junit报告后执行语句?
我正在将testng生成的junit报告转换为其他格式 为此,我编写了以下代码:如何仅在TESTNG生成junit报告后执行语句?,junit,automated-tests,testng,Junit,Automated Tests,Testng,我正在将testng生成的junit报告转换为其他格式 为此,我编写了以下代码: @AfterTest public void execute() { String junitReport = "TEST-"+this.getClass().getCanonicalName()+".xml"; TestManagerLogger obj = new TestManagerLogger(); obj.convertLog(junitReport); } 但这不起作用,因
@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();
}
}
@Test(dataProvider=“jobCount”)
public void testJobCount(字符串方案、字符串URL、字符串方法类型、字符串状态){
URL=URL.replaceFirst(“ip”,ip);
字符串logonToken=logonUtitility.logon();
字符串结果=ResponseGenerator.response(URL、logonToken、methodType);
列表值RomeXcel=StringSplitter.getKeyValuePairs(状态);//返回包含键值的hashmap,例如:failed=0,total=3
列表值fromrest=new ArrayList();
文档文档=StringSplitter.convertStringToDocument(结果);
javax.xml.xpath.xpath xpath=XPathFactory.newInstance().newXPath();
NodeList节点,node1;
试一试{
node=(NodeList)xPath.evaluate(“/feed/entry/content/attrs/attr[@name='status\u type']”,doc,XPathConstants.NODESET);
node1=(NodeList)xPath.evaluate(“/feed/entry/content/attrs/attr[@name='count']”,doc,XPathConstants.NODESET);
HashMap hm=新的HashMap();
对于(int i=0;i您应该做的是实现您自己的reporter:
公共类TestManagerReporter实现IReporter{
公共void生成器报告(列表xmlSuites、列表suites、字符串输出目录){
//印刷品
适用于(ISuite:suites){
对于(IInvokedMethod:suite.getAllInvokedMethods()){
if(方法.isTestMethod()){
ITestResult result=method.getTestResult();
if(result.getStatus()==成功){
//印刷品
//印刷品
//打印结果。getName()
//印刷品
//印刷品
//打印结果.getEndMillis()-result.getStartMillis()
//印刷品
//印刷品
}
}
}
}
//印刷品
}
}
您必须使用像jenkins这样的CI来实现这一点,因为它提供了执行后期构建之类的操作的选项。因为testng@PostTest也是执行的一部分,所以它永远不会理解您想要的方式。谢谢您的建议。我看了文档,但从那里做不了什么。如果您能告诉我在哪里可以找到d,那将非常有帮助这个想法是从TestNG模型对象生成您自己的格式,而不是使用中间格式(junit)。如果您需要帮助,您应该共享所需的格式。我在问题中添加了所需的xml报告格式。好的,我添加了一个示例,说明如何生成所需的格式。还有许多其他方法可以做到这一点,只需检查TestNG API。我让您完成xml生成部分。感谢您的示例,我们开始研究这一部分。我只想再问你一个问题,我正在使用eclipse运行我的测试类,而不配置testng.xml,这个类在测试用例执行后会自动调用吗?
<logfile>
<logrecord>
<case>scenario</case>
<etime>Execution time</etime>
</logrecord>
</logfile>
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>
}
}