Writing Tests

It is time to test the response you received. Change your specification file scanapi.yaml to have the following content:

endpoints:
  - name: snippets-api
    path: http://demo.scanapi.dev/api/v1/
    headers:
      Content-Type: application/json
    requests:
      - name: health
        method: get
        path: /health/
        tests: # this is new
          - name: status_code_is_200 # this is new
            assert: ${{ response.status_code == 200 }} # this is new
          - name: body_equals_ok # this is new
            assert: ${{ response.json() == "OK!" }}  # this is new

Run ScanAPI again:

$ scanapi run

Reload your browser and check the TESTS now:

Test details

Inside the ${{ }} notation, you can write pure Python Code. response is a requests.Response object containing the response information of the request.

Note that the Tests Summary brings some useful information about the tests now. If anything goes wrong or if any test fails, scanapi command will return an error with the corresponding exit code.

Tests summary

Congrats, you have documented and tested your first request! Now, it is time to start testing endpoints that need authentication.