svanoort / pyresttest

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

How make secuencial tests with pyresttest? #269

Open macagua opened 6 years ago

macagua commented 6 years ago

Hi @svanoort

I trying to make seven secuencial tests from the API Rest that I need to test.

The first test is a Get request that generate a response in Json format, from which I have to extract and resend two or three values to the next test, to attach those values as parameters of the URL of the next Get query and so on...

Here a example of my code:

# Usage: resttest.py --url http://localhost:8089/ --test ./tests/api/test_cars.yaml

---
- config:
    - testset: "Tests App"
    - timeout: 60000

- test:
    - name: "GET - cars list"
    - url: "/api/cars/ARG/2018-2010/SPORT/"
    - headers: {
                'Content-Type': 'application/json',
                'Authorization': 'apikey 3fdty0dsgd454lrrfsdgv8455safmmkj544wdss3',
                'fc-user-id': '09876',
                'fc-api-key': 'sdf4534lkjoj4564789kh3423ñlñmkn2232mkpb3'
      }
    - extract_binds:
        - 'id': {'jsonpath_mini': 'objects.id'}
        - 'car_code': {'jsonpath_mini': 'objects.cars.0.car.code'}
        - 'shash_code': {'jsonpath_mini': 'objects.cars[0].rates[0].shash'}
    - expected_status: [200]

- test:
    - name: "GET - car_info"
    # http://localhost:8089/api/car_info/ARG00OS5?language=en&id=4565609876098702323
    - url: {'template': "/api/car_info/$car_code?language=es&id=$id"}
    - headers: {
                'Content-Type': 'application/json',
                'Authorization': 'apikey 3fdty0dsgd454lrrfsdgv8455safmmkj544wdss3',
                'fc-user-id': '09876',
                'fc-api-key': 'sdf4534lkjoj4564789kh3423ñlñmkn2232mkpb3'
      }
    - expected_status: [200]

I execute the following command:

$ resttest.py --verbose --url http://localhost:8089/ --test ./tests/api/test_cars.yaml --interactive true --print-bodies true --log debug

Then the first test show INFO:Test Succeeded

But the second test show the error:

           </li>\n        \n      </ol>\n      <p>The current URL, <code>api/cars/$car_code</code>, didn't match any of these.</p>\n    \n  </div>\n\n  <div id=\"explanation\">\n    <p>\n      You're seeing this error because you have <code>DEBUG = True</code> in\n      your Django settings file. Change that to <code>False</code>, and Django\n      will display a standard 404 page.\n    </p>\n  </div>\n</body>\n</html>\n", "response_headers": [["server", "nginx"], ["date", "Wed, 03 Jan 2018 20:56:11 GMT"], ["content-type", "text/html"], ["transfer-encoding", "chunked"], ["connection", "close"], ["vary", "Accept-Language, Cookie"], ["x-frame-options", "SAMEORIGIN"], ["content-language", "en"]], "response_code": 404, "passed": false, "test": {"templates": null, "expected_status": [200], "_headers": {"fc-api-key": "sdf4534lkjoj4564789kh3423ñlñmkn2232mkpb3", "Content-Type": "application/json", "fc-user-id": "09876", "Authorization": "apikey 3fdty0dsgd454lrrfsdgv8455safmmkj544wdss3"}, "group": "Default ", "name": "GET - car_info", "_url": "http://localhost:8089/api/cars/$car_code?language=es&request_id=$id", "templated": {}}, "failures": [{"failure_type": "Invalid HTTP Response Code", "message": "Invalid HTTP response code: response code 404 not in expected codes [[200]]", "validator": null, "details": null}]}
ERROR:Test Failed: GET - car_info URL=http://localhost:8089/api/cars/$car_code?language=es&request_id=$id Group=Default HTTP Status Code: 404
ERROR:Test Failure, failure type: Invalid HTTP Response Code, Reason: Invalid HTTP response code: response code 404 not in expected codes [[200]]
===================================
Test Group Default  FAILED: : 1/2 Tests Passed!

Any idea how I can resolve this error?

macagua commented 6 years ago

@svanoort I solved this issue with the curl option curl_option_followlocation: True and curl_option_maxredirs: 2 like the following code:

# Usage: resttest.py --url http://localhost:8089/ --test ./tests/api/test_cars.yaml

---
- config:
    - testset: "Tests App"
    - timeout: 60000

- test:
    - name: "GET - cars list"
    - url: "/api/cars/ARG/2018-2010/SPORT/"
    - curl_option_followlocation: True
    - curl_option_maxredirs: 2
    - headers: {
                'Content-Type': 'application/json',
                'Authorization': 'apikey 3fdty0dsgd454lrrfsdgv8455safmmkj544wdss3',
                'fc-user-id': '09876',
                'fc-api-key': 'sdf4534lkjoj4564789kh3423ñlñmkn2232mkpb3'
      }
    - extract_binds:
        - 'id': {'jsonpath_mini': 'objects.id'}
        - 'car_code': {'jsonpath_mini': 'objects.cars.0.car.code'}
        - 'shash_code': {'jsonpath_mini': 'objects.cars[0].rates[0].shash'}
    - expected_status: [200]

- test:
    - name: "GET - car_info"
    # http://localhost:8089/api/car_info/ARG00OS5?language=en&id=4565609876098702323
    - url: {'template': "/api/car_info/$car_code?language=es&id=$id"}
    - curl_option_followlocation: True
    - curl_option_maxredirs: 2
    - headers: {
                'Content-Type': 'application/json',
                'Authorization': 'apikey 3fdty0dsgd454lrrfsdgv8455safmmkj544wdss3',
                'fc-user-id': '09876',
                'fc-api-key': 'sdf4534lkjoj4564789kh3423ñlñmkn2232mkpb3'
      }
    - expected_status: [200]
benhowes commented 6 years ago

Seems that you've got this sorted now anyway @macagua, but we've made Tavern specifically to make sequential tests easier. https://github.com/taverntesting/tavern