Checking whether a response contains a list of values in Robot Framework?

14,217

Solution 1

Should Contain keyword does not accept a list type variable as 2nd parameter. You can make a workaround by using a FOR loop to parse through list:

*** Variables ***
${Response}    "250 hello world foobar"

*** Test Cases ***
Stackoverflow
@{list} =   Create List   hello   world    250
:FOR    ${item}    in     @{list}
\       Should Contain    ${RESPONSE}    ${item}

Solution 2

The collections library has what you want

Library  Collections
List Should Contain Sub List  ${list1}  ${list2}

Fails if not all of the elements in list2 are found in list1.

Share:
14,217
shicky
Author by

shicky

Currently working as a software engineer for Puppet. Award-winning Computer Science graduate with experience as a developer, a support engineer, a tester and a shelf stacker! But right now I'm fascinated by efficiency and love to optimise feedback systems to be quicker, offer more clarity, as well as proving more reliable. Enjoy the variety that working as a tester provides most of all. In the same day I can find myself head down in an IDE, having high-level discussions about the effectiveness of an upcoming feature to address business needs and looking over analytics to see where we should focus our testing efforts. I like to learn while helping others and do so by co-organising the NI Testers meetup as well as helping out at Code Club Holywood. You can find me on twitter @Shicky4 or my blog www.nicholaspshaw.com

Updated on June 05, 2022

Comments

  • shicky
    shicky almost 2 years

    I'm new to robot so apologies if this is a stupid question, but I'm looking for means to pass a list to the built in method should_contain:

    def should_contain(self, item1, item2, msg=None, values=True):
        """Fails if `item1` does not contain `item2` one or more times.
    
        Works with strings, lists, and anything that supports Python's `in`
        keyword. See `Should Be Equal` for an explanation on how to override
        the default error message with `msg` and `values`.
    
        Examples:
        | Should Contain | ${output}    | PASS |
        | Should Contain | ${some_list} | value  |
        """
        msg = self._get_string_msg(item1, item2, msg, values, 'does not contain')
        asserts.fail_unless(item2 in item1, msg)
    

    so first the simple method in a test case, I know this syntax is wrong but is it possible to do something similar to:

    Should Contain    ${RESPONSE}    "hello","world",250
    

    I tried using a list variable and passing it in, this seemed to work but on deeper investigation it seems to just compare an element count rather than the actual element values which was disappointing

    To get around the problem I just did Should Contain on the values I cared about. The problem is I'm repeating Should Contain with different data on several lines, which clearly isn't good practice.

    Should Contain  ${RESPONSE}    "Hello"
    Should Contain  ${RESPONSE}    "World"
    Should Contain  ${RESPONSE}    250
    

    Is anybody able to offer some guidance or an improved method of achieving what I'm trying to do? I was considering creation of a new method to allow it into a custom library but I figured there has to be a more simple way. To be clear I don't care about order of the items.

  • qbert220
    qbert220 about 6 years