如果任何断言失败,请保存测试用例属性

时间:2017-03-31 12:35:59

标签: groovy soapui

如果在这个groovy脚本步骤中任何断言失败,如何保存测试用例属性?

以下是示例代码:

// define properties required for the script to run. 
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) 
def dataFolder = groovyUtils.projectPath

def vTIDAPI = testRunner.testCase.getPropertyValue("vTIDAPI")
def vTIDDB = testRunner.testCase.getPropertyValue("vTIDDB")
def RefAPI = testRunner.testCase.getPropertyValue("RefAPI")
def RefDB = testRunner.testCase.getPropertyValue("RefDB")
def AmountAPI = testRunner.testCase.getPropertyValue("AmountAPI")
def AmountDB = testRunner.testCase.getPropertyValue("AmountDB")
def CurrencyAPI = testRunner.testCase.getPropertyValue("CurrencyAPI")
def CurrencyDB = testRunner.testCase.getPropertyValue("CurrencyDB")


assert vTIDAPI == vTIDDB
assert RefAPI == RefDB
assert AmountAPI == AmountDB
assert CurrencyAPI == CurrencyDB

1 个答案:

答案 0 :(得分:1)

这是Groovy Script,它比较给定的属性集和任何断言失败,将属性写入给定文件。

  • 您需要更改要为变量propFileName变量存储的属性文件名的值。
  • 如果需要,以key:值对格式添加更多要声明的属性
//Provide / edit the file name to store properties
def propFileName = '/tmp/testCase.properties'

//Define the properties to be matched or asserted ; add more properties if needed
def props = [ 'vTIDAPI':'vTIDDB', 'RefAPI':'RefDB', 'AmountAPI': 'AmountDB', 'CurrencyAPI': 'CurrencyDB']

/**
 * Do not edit beyond this point
 */
def writeTestCasePropertiesToFile = {
    //Get the test case properties as Properties object
    def properties =  context.testCase.properties.keySet().inject([:]){map, key ->  map[key] = context.testCase.getPropertyValue(key); map as Properties}  
    log.info properties
    assert properties instanceof Properties
    properties?.store(new File(propFileName).newWriter(), null) 
}

def myAssert = { arg1, arg2 -> 
    context.testCase.getPropertyValue(arg1) == context.testCase.getPropertyValue(arg2) ? null : "${arg1} value does not match with ${arg2}"
}
def failureMessage = new StringBuffer()
props.collect{ null == myAssert(it.key, it.value) ?: failureMessage.append(myAssert(it.key, it.value)).append('\n')}
if(failureMessage.toString()) {
    log.error "Assertion failures:\n ${failureMessage.toString()}"
    writeTestCasePropertiesToFile()
    throw new Error(failureMessage.toString())
} else {
    log.info 'Assertions passed'
}

编辑:基于OP评论

def myAssert = ...替换为以下代码片段。

def myAssert = { arg1, arg2 -> 
    def actual = context.testCase.getPropertyValue(arg1)
    def expected = context.testCase.getPropertyValue(arg2)
    actual == expected ? null : "${arg1} value does not match with ${arg2} - api ${actual} vs db ${expected}"
}