taverntesting / tavern

A command-line tool and Python library and Pytest plugin for automated testing of RESTful APIs, with a simple, concise and flexible YAML-based syntax
https://taverntesting.github.io/
MIT License
1.02k stars 192 forks source link

Support for YAML Indexing and Advanced Assertions #895

Closed varsharyalii closed 2 months ago

varsharyalii commented 9 months ago

Description: Consider a scenario where there is a list of items with quantities, and the goal is to assert the quantity of a specific item in a specific position without triggering a pass due to the presence of the same quantity in another position.

- test: Verify quantity for the third item
  stages:
    - response:
        status_code: 200
        json:
          items:
            - name: "Item1"
              quantity: 5
            - name: "Item2"
              quantity: 10
            - name: "Item3"
              quantity: 5

The provided example should only match the quantity of the third item and not inadvertently pass due to the presence of the same quantity elsewhere in the list.

I wish there was someplace I could add !index=2 or something as such This is especially causing a blocker while trying to run negative tests

michaelboulton commented 7 months ago

You can use jmespath assertions like https://github.com/taverntesting/tavern/blob/4396844435a74d0af4c06ca64eb1c15cf02b61ab/tests/integration/test_jmes.tavern.yaml#L215-L225 to do this but for specific situations like this it could also just be easier to write a specific external function to check this case.