2017-03-16 97 views
1

我正在运行测试用例并使用groovy断言数据。我想打印每个失败的消息到html junit generate report如何将所有断言失败消息打印到SoapUI的HTML报告中

示例代码

import groovy.json.JsonSlurper 

    def ResponseMessage = messageExchange.response.responseContent 
    def jsonString = new JsonSlurper().parseText(ResponseMessage) 

    assert !(jsonString.isEmpty()) 
    assert jsonString.code == 200 
    assert jsonString.status == "success" 

    def accountInfo = jsonString.data 
    assert !(accountInfo.isEmpty()) 

    def inc=0 

    //CHECKING LANGUAGES IN RESPONSE 
    if(accountInfo.languages.id!=null) 
    { 

      log.info("Language added successfully") 
    } 
    else 
    { 

     log.info("Language NOT added.") //want to display this in html report 
     inc++ 

    } 

    if(accountInfo.educations!=null) 
     { 

     log.info("Educations added successfully") 
     } 
    else 
    { 

    log.info("Educations NOT added.") //want to display this in html report 
    inc++ 

    } 

assert inc<=0,"API unable to return all parameters, Please check logs" 

方案

我在做什么在这里,如果测试条件不匹配,去ELSE,我做可变INC的增量所以最后如果我的测试失败,如果inc> 0。

报告

在JUnit风格HTML生成的报告,如果测试失败就只显示所谓API unable to return all parameters, Please check logs

一个消息,但我想要的是显示每个IF状态信息转换成HTML报告如果任何条件进入或其他部分。

回答

1

夫妇指针:

  • 断言停止在第一次失败并且只有这个失败消息是junit的报告的一部分。
  • 已经说过,用户不知道当前响应是否存在任何进一步的验证失败。
  • 是的if..else部分消息是不是为了实现这一目标,需要收集所有这些信息,最后显示收集到的错误消息junit report
  • 一部分。
  • 下面的解决方案使用变量messages并附加每个允许在最后显示它们的故障。这样,如果OP所要求的是可取的,那么所有的失败都可以在报告中显示出来。
  • 用户也可以使用下面的语句除了assert声明

    if (messages) throw new Error(messages.toString())显示在报告中的信息

脚本断言

import groovy.json.JsonSlurper 


//check if the response is empty 
assert context.response, 'Response is empty or null' 

def jsonString = new JsonSlurper().parseText(context.response) 

def messages = new StringBuffer() 
jsonString.code == 200 ?: messages.append("Code does not match: actual[${jsonString.code}], expected[200]\n") 
jsonString.status == "success" ?: messages.append("Status does not match: actual[${jsonString.status}], expected[success]\n") 

def accountInfo = jsonString.data 
accountInfo ?: messages.append('AccountInfo is empty or null\n') 

def inc=0 

//CHECKING LANGUAGES IN RESPONSE 
if(accountInfo.languages.id) { 
    log.info('Language added successfully') 
} else { 
    log.error('Language NOT added.') //want to display this in html report 
    messages.append('Language not added.\n') 
    inc++ 
} 

if(accountInfo.educations) { 
    log.info('Educations added successfully') 
} else { 
    log.error('Educations NOT added.') //want to display this in html report 
    messages.append('Educations NOT added.\n') 
    inc++ 
} 

//inc<=0 ?: messages.append('API unable to return all parameters, Please check logs.') 
//if(messages.toString()) throw new Error(messages.toString()) 
assert inc<=0, messages.append('API unable to return all parameters, Please check logs.').toString() 
+0

你给出的解决方案工作正常,我。只有一个问题,即使断言失败,它也不会失败我的测试用例。直到声明一切正常。但是当我看着junit的html报告。它显示status = pass。我想要的是当断言失败时报告应该失败,并在报告中打印所有断言消息以查看失败。 –

+0

你用过吗?它是否显示了您的所有信息?你有没有设法从SoapUI本身尝试它? – Rao

+0

只是在回答中将if(messages)'改为'if(messages.toString())'。你可以试试这个更新的答案吗? – Rao

相关问题