我正在运行测试用例并使用groovy声明数据。我想将每条失败的邮件打印到html junit generate report
。
示例代码
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"
方案
我在这里做的是,如果IF测试条件不匹配并转到 ELSE ,我将变量 inc 增加1.所以最后如果失败我的测试,如果inc> 0。
报告
在junit样式的html生成报告中,如果测试失败,则只显示一条名为API unable to return all parameters, Please check logs
的消息
但我想要的是,如果任何条件进入 ELSE 部分,则将每个 IF 条件消息显示到HTML报告中。
答案 0 :(得分:1)
指点:
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()