cdent / gabbi

Declarative HTTP Testing for Python and anything else
http://gabbi.readthedocs.org/
Other
148 stars 34 forks source link

response_json_paths not equals #263

Open m-barthelemy opened 5 years ago

m-barthelemy commented 5 years ago

Is is currently possible to ensure that a field in the response is not equal to a value?

I know I can do

response_json_paths:
          $.name: hello

to check for equality, but is there a way I can ensure the opposite ($.name is not equal, or different from, hello?)

cdent commented 5 years ago

Initially I thought you were after something like #227 but I see that you want the field to be present but have a a different value. I assume you don't know the new value, just that it is different from hello.

Probably the best way to do this is with a negative assertion in a regex:

  response_json_paths:
      $.name: /^(?!hello)/

That will need some tweaking if you're expecting the new data to be a substring that might include hello.

Other ideas that might be worth exploring, below:

Depending on the structure of your data you might be able to use len (see https://gabbi.readthedocs.io/en/latest/jsonpath.html ) to filter a list and checks its length.

Another option is to mark a test xfail, so that if it succeeds that's a failure:

tests:                                                                         
- name: check for value                                                        
  xfail: True                                                                  
  POST: /                                                                      
  verbose: all                                                                 
  request_headers:                                                             
      content-type: application/json                                           
  data:                                                                        
      items:                                                                   
          - name: cow                                                          
            sound: moo                                                         
  response_json_paths:                                                         
      $.items[?name = "horse"].sound: not here

That, however, requires a test running that treats unexpected success as a failure (not all do).

Let me know which of these work, or if none of them do. If you're happy please close the issue.