2017-03-16 114 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