svanoort / pyresttest

Python Rest Testing
Apache License 2.0
1.15k stars 326 forks source link

Weird behaviour during comparision of values #294

Open oursinyi opened 5 years ago

oursinyi commented 5 years ago

@svanoort

Hi There,

I faced with weird behaviour when I tried co compare two values. The testset:

- config:
   - testset: "User related default tests"
   - generators:
        - "id": {'type': "random_text", 'length': "20", 'character_set': "alphanumeric"}

- test:
   - name: "login"
   - group: "cust_login"
   - url: "/authenticate/login"
   - method: "POST"
   - headers: {template: {'Authorization': 'Basic ABCDEFG=='}}
   - expected_status: [201]
   - extract_binds:
       - "token": {"jsonpath_mini": "session_id"}

- test:
   - name: "user list"
   - group: "user_list"
   - url: "/users/list/"
   - method: "GET"
   - headers: {template: {'Authorization': "OAuth $token"}}
   - expected_status: [200]
   - extract_binds:
       - "totalnums": {"jsonpath_mini": "total_num"}
       - "name": {"jmespath": "users[0].username"}

{ more tests here (user creation) }

- test:
   - name: "user list"
   - group: "user_list"
   - url: "/users/list/"
   - method: "GET"
   - headers: {template: {'Authorization': "OAuth $token"}}
   - expected_status: [200]
   - validators:
       - compare: {jsonpath_mini: "total_num", comparator: "gt", expected: {template: "$totalnums"}}

{ more tests here (user deletion) }

- test:
   - name: "user list"
   - group: "user_list"
   - url: "/users/list/"
   - method: "GET"
   - headers: {template: {'Authorization': "OAuth $token"}}
   - expected_status: [200]
   - validators:
       - compare: {jsonpath_mini: "total_num", comparator: "eq", expected: {template: "$totalnums"}}

The first comparation's result: ERROR:Test Failure, failure type: Validator Failed, Reason: Comparison failed, evaluating gt(8, 7) returned False

And the second comparision's result: ERROR:Test Failure, failure type: Validator Failed, Reason: Comparison failed, evaluating eq(7, 7) returned False

I am not sure why get False if 8 grater than 7 and 7 equal with 7? (These data are from same field, which means these are same type)

Thanks in advance!

kovacsbalu commented 5 years ago

Hi @oursinyi, you can create a custom_comparators.py and use with --import_extensions "custom_comparators"

COMPARATORS = {
    'int_lt': lambda a, b: int(a) < int(b),
    'int_gt': lambda a, b: int(a) > int(b),
    'int_eq': lambda a, b: int(a) == int(b)
}

Now you can comapre json data and template variable as integer. - compare: {jsonpath_mini: "total_num", comparator: "int_gt", expected: {template: "$totalnums"}}

Pacsi ;)