What is the difference between 'Should Be Equal' and 'Should Match'?

Both Should Be Equal and Should Match are used for verification in Robot Framework, but they serve different purposes and have distinct ways of comparing values:

Should Be Equal:

  • Purpose: This keyword performs a strict equality check between two values. It verifies that the actual value is exactly the same as the expected value.
  • Comparison: It compares the values directly, and they must be identical for the verification to pass. This includes checking for the same data type and value.
  • Case sensitivity: By default, Should Be Equal is case-sensitive for strings.
  • Example :
  • ${actual_value}    Set Variable    Hello
    ${expected_value}    Set Variable    Hello
    Should Be Equal    ${actual_value}    ${expected_value}    # Pass
    
    ${actual_value}    Set Variable    hello
    ${expected_value}    Set Variable    Hello
    Should Be Equal    ${actual_value}    ${expected_value}    # Fail (case-sensitive)

Should Match:

  • Purpose: This keyword uses pattern matching to compare a string against a pattern. It's more flexible than Should Be Equal and is useful for verifying that a string conforms to a certain format or contains specific substrings.
  • Comparison: It uses regular expression matching to check if the string matches the given pattern. You can use regular expression syntax to define the pattern, allowing for partial matches, wildcards, and more complex matching criteria.
  • Case sensitivity: By default, Should Match is case-insensitive. You can use flags in your regular expression to make it case-sensitive if needed.
  • Example :
  • ${actual_value}    Set Variable    Hello, world!
    ${pattern}       Set Variable    Hello.*
    Should Match    ${actual_value}    ${pattern}    # Pass (matches the pattern)
    
    ${actual_value}    Set Variable    hello, world!
    ${pattern}       Set Variable    Hello.*
    Should Match    ${actual_value}    ${pattern}    # Pass (case-insensitive by default)
    
    ${actual_value}    Set Variable    hello, world!
    ${pattern}       Set Variable    (?i)Hello.*  # (?i) flag for case-insensitive matching
    Should Match    ${actual_value}    ${pattern}    # Pass (case-insensitive)

Key Differences:

Feature Should Be Equal Should Match
Comparison Strict equality Pattern matching (regular expressions)
Flexibility Less flexible More flexible
Use cases Exact value matching Partial matching, format verification
Case sensitivity Case-sensitive (by default) Case-insensitive (by default)