Search code examples
robotframeworkbuilt-in

Robot Framework : BuiltIn library : Should Match : How to pass a pattern parameter correctly?


Given the following code:

*** Test Cases ***
Use "Should Match"
[Documentation]     Should Match    string, pattern, msg=None, values=True, ignore_case=False
...                 Fails if the given string does not match the given pattern.
...                 Pattern matching is similar as matching files in a shell with *, ? and [chars] acting as
...                 wildcards. See the Glob patterns section for more information.

Should Match        string=Can find me here   pattern=me   msg='The overwriting error'  # fails unexpectedly
Should Match        string='Will match with star'   pattern=*
Should Match        string='Will match this'        pattern=[atx]his      # fails unexpectedly
Should Match        string='Will match with keyword'    pattern=?eyword   # fails unexpectedly

The goal is to make all the statements in the test case pass. Currently, the first statement fails with the error:

'The overwriting error': 'Can find me here' does not match 'me'


Solution

  • When using Should Match the pattern needs to match the whole string, not just part of the string. If you want the first pattern to pass, you need to change it to *me*. The first star will match everything up to the word "me", and the second star will match everything after.

    The same is true for the other patterns. If you're looking for a pattern inside a larger string you need to add * on either side of the pattern to match all of the other characters.

    *** Test Cases ***
    Use "Should Match"
        Should Match        Can find me here           pattern=*me*
        Should Match        'Will match with star'     pattern=*
        Should Match        'Will match this'          pattern=*[atx]his*
        Should Match        'Will match with keyword'  pattern=*?eyword*