asyrjasalo / RESTinstance

Robot Framework library for RESTful JSON APIs
https://pypi.org/project/RESTinstance
GNU Lesser General Public License v3.0
206 stars 84 forks source link

String keyword to accept partial strings for field validation instead of the full and possibly long strings that have to be exactly the same as in the JSON field contents for it to PASS #12

Closed sqilz closed 6 years ago

sqilz commented 6 years ago

Written in Robot Framework

The /users/1 name field contains "Leanne Graham"

String Field Partial
    [Documentation]      Check if a field contains a part of a string
    ...                  can use many strings in @{text} separated by two spaces
    [Arguments]          ${field_name}    @{text}
        ${string}        String    response body ${field_name}
                         Should Contain Any    ${string}    @{text}

In a test case

        [Tags]  partial-string
        GET                   /users/1
        Validate.String Field partial  name    Lea  rrggrgr  Lxxel  street  city    **PASS**
        String  name    Lea                                **FAIL**
        String  name    Lea  rrggrgr  Lxxel  street  city  **FAIL**
asyrjasalo commented 6 years ago

You can use JSON Schema keyword pattern for testing if a JSON string contains something:

*** Settings ***
Library         REST

*** Test Cases ***
Use JSON Schema keyword pattern to test JSON string for substrings
    [Setup]     GET     https://jsonplaceholder.typicode.com/users/1

    # matches "Leanne Graham" as a substring:
    String      response body name        pattern="anne Grah"

    # quotes are not necessary, it is still read as a string by the library:
    String      response body name        pattern=anne Grah

    # pattern supports regular expressions:
    String      response body name        pattern=(sqilz|Leanne Graham)

    # checking that 'name' does not contain the word:
    String      response body name        pattern=^((?!asyrjasalo).)*$

(pattern ref: http://json-schema.org/draft-04/json-schema-validation.html#rfc.section.5.2.3)

Closing this issue. Please open a new issue if this did not answer the use case, or I missed something. Also apologies for a (very likely) late reply.

ec-wagner commented 5 years ago

wow, that's a cool feature! seems that it is not documented anywhere in this repo (besides in this issue) ... I just found it by chance because I wanted to check existing issues before filing a new one that I have with the String keyword.

this pattern feature is definitely worth to be documented ;-)