简体   繁体   English

Groovy map 比较一个测试步骤的密钥和另一个测试步骤的值,并断言它是真还是假

[英]Groovy map compare key from one test step and value of another test step and assert if it's true or false

I have groovy script like this:我有这样的 groovy 脚本:

def map = ['Response' : 'id']

def p1 = context.testCase.testSteps["TestStep_1"].properties
def p2 = context.testCase.testSteps["TestStep_2"].properties

def result = []

def assertPropertyValue = { p1key, p2key -> 
    def temp = p1[p1key].value == p2[p2key].value
    log.info("Comparing $p1key, and $p2key values respectively ${p1[p1key].value} == ${p2[p2key].value} ? $temp")
    temp
}
map.each { result << assertPropertyValue(it.key, it.value) }
assert result.each{it.value == true}, 'Comparison failed, check log'

My groovy script does not assert false despite value of TestScript_2 does not match with key from TestStep_1.尽管 TestScript_2 的值与 TestStep_1 中的键不匹配,但我的 groovy 脚本并未断言为 false。 I get the following log:我得到以下日志:

Fri Aug 12 17:48:16 CEST 2022:INFO:Comparing Response, and id values respectively {"code":"200","timestamp":"Fri Aug 12 15:12:45 UTC 2022","HttpStatus":"OK","id":"8154b2d1-4f83-4b2c-b100-5dab36b37ab6"} == 929c2a62-5c8a-4e85-bf65-776696503818 ? false

IDs does not match and I will expect my groovy test case to assert with "'Comparison failed, check log'". ID 不匹配,我希望我的 groovy 测试用例断言“比较失败,检查日志”。 Do you have any idea why this happens?你知道为什么会这样吗?

result.each{} returns the result object. result.each{}返回result object。

so, result.each{it.value == true} returns "true" if result is not empty:因此,如果result不为空,则result.each{it.value == true}返回“true”:

this should work:这应该工作:

assert !result.find{it.value == false}, 'Comparison failed, check log'

each on a List in groovy return a List so since your result object is not null neither empty, then your assert always returns true . each在 groovy List中的每个都返回一个List ,因此由于您的result object 不是null既不是空的,那么您的断言总是返回true

If you want to check that there is no false value inside result list, try with any :如果要检查result列表中是否没有false值,请尝试使用any

assert !result.any{ it.value == false }, 'Comparision failed, check log'

Alternatively you can use find as @dagget suggest in his answer, however since find returns the object found or null it could be necessary an extra comparission to help assert to discern bettwen boolean object false and null . Alternatively you can use find as @dagget suggest in his answer, however since find returns the object found or null it could be necessary an extra comparission to help assert to discern bettwen boolean object false and null . Using find :使用find

assert result.find { it.value == false } == null, 'Comparision failed, check log'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM