简体   繁体   中英

Compare list of dictionary in Robot Framework

I have two list of list of dictionary and i want to compare the vale of first list dictionary to second list of dictionary

For example:

Dictionary A contains [{Name:C}, {Name:A}, {Name:B}]
Dictionary B contains [{Name:A}, {Name:B}, {Name:C}]

How to take A's 1st Dictionary {Name:C} and check if it exists in Dictionary B.

您想查看某些字典列表是否包含至少一个映射'name': 'C'字典?

any(d['name'] == 'C' for d in list_of_dict if 'name' in dict)

If I understand your question correctly, you should be able to do this using the built in Collections library. This code took the values in one dictionary and checked to see if the value exists in the other.

*** Settings ***
Library  Collections

*** Variables ***
&{DICTONARY_ONE} =  name1=a  name2=b  name3=c  name4=d
&{DICTONARY_TWO} =  name1=c  name2=a  name3=d  name4=b

*** Test Cases ***
Dictonary Test
    @{dictonary2_list} =  Get Dictionary Values  ${DICTONARY_TWO}
    :For  ${item}  in  @{dictonary2_list}
    \   Dictionary Should Contain Value  ${DICTONARY_ONE}  ${item}

If this is not quite what you are after, there should be something in collections that will let you do what you need. Here is a link to the documentation.
http://robotframework.org/robotframework/latest/libraries/Collections.html

I hope that helps.

Be it a dictionary or string, as long as it is a part of list it can be compared.

*** Settings ***
Library  Collections

*** Test Case ***
Dictionary Validation
    ${dict1}=   Create Dictionary   Name    A
    ${dict2}=   Create Dictionary   Name    B
    ${dict3}=   Create Dictionary   Name    C       
    @{li}=  Create List  ${dict3}  ${dict1}  ${dict2}
    @{lj}=  Create List  ${dict1}  ${dict2}  ${dict3}

    :For  ${item1}  in  @{li}
        \   List Should Contain Value  ${lj}  ${item1}  

Just in-case the comparison is on a part of dictionary (not complete dictionary), we need to think of another way. Let me know if it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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