drs
drs

Reputation: 5797

How do I bind the output of a request for use in a subsequent test in pyresttest?

I am using pyresttest to test my api endpoints. I would like to bind the output of one test to a variable that can be used in a subsequent test. I need this in order to test endpoints like /object/{id} where ID is a hash generated by MongoDB. This hash is random and changes each time the database is seeded. I don't see any way of testing this endpoint without seeding the database, searching for a specific entry, binding the ID of that entry, and then using that to test the endpoint.

Upvotes: 1

Views: 757

Answers (1)

drs
drs

Reputation: 5797

Saving the output of one test is achieved with the extract_binds element. There is an example of its use in the pyrestest repository in pyresttest/examples/miniapp-extract-validate.yaml:

- config:
    - testset: "Demonstrate use of extract after creating a person"
- test: # create entity by POST
    - name: "Create person"
    - url: "/api/person/"
    - method: "POST"
    - body: '{"first_name": "Test","last_name": "User","login": "testuser"}'
    - headers: {Content-Type: application/json}
    - expected_status: [201]
    - extract_binds:
        - 'id': {'jsonpath_mini': 'id'}
- test:
    - name: "Get person you just created and validate them"
    - url: {'template': "/api/person/$id/"}
    - validators:
        - compare: {jsonpath_mini: 'id', comparator: 'str_eq', expected: {template: '$id'}}
        - extract_test: {jsonpath_mini: 'login', test: 'exists'}

Upvotes: 2

Related Questions