Subeshb1 Api Test Versions Save

🌿 A simple bash script to test JSON API from terminal in a structured and organized way.

v0.3.3

3 years ago

Fixed a bug for path_eq comparison where when a string value is compared by a path, an error is thrown by jq. Error Link: https://github.com/subeshb1/api-test/issues/27. Special thanks to @badevos for the fix

v0.3.2

3 years ago

Fixed script breaking on test cases starting with numbers Eg: 01_testcase, 02_testcase and containing hyphen - Eg: test-case

v0.3.1

3 years ago

v0.3.0

3 years ago

Changes

  • Added external script usage while testing
  • Added dependency installation check
  • Search test.json, api-test.json and template.json by default if no file is found
  • Add super silent mode
  • Organized spec view
  • Show test results
  • Support CI/CD for automated testing

v0.2.0

3 years ago

To run an automated test run,

api-test -f test.json test test_case_1
api-test -f test.json test all # To run all tests

test

Both the headers and body can be compared to create automated api tests using different types of checking schemes described in further sections. All the checking schemes can be used for a test case. To define test units add them in expect object in the testCase.

{
  "test_case_1": {
    "path": "/path_1",
    "method": "POST",
    "expect": { // automated tests are kept inside this object
      "header": {
        ...
      },
      "body": {
        ...
      }
    }
  }
}

There are 5 ways you can compare the result from the api response.

1. eq

The eq check compares every element in an object irrespective of the order of object keys and array elements. Every element in compared object should match as the object defined in eq block.

Syntax

{
  ...
  "expect": {
    "body": {
      "eq": {
        "key": "value"
      }
    }
  }
}

Example: The api has following response.

{
  "name": "ram",
  "age": 20
}

To test using eq check:

{
  ...
  "expect": {
    "body": {
      "eq": {
        "name": "ram",
        "age": 20
      }
    }
  }
}

The check will pass for the above response. If any of the value or key is different it will throw error.

2. contains

The contains check compares the expected value with all the possible subset of the compared object irrespective of the order of object keys and array elements. It will pass if the value matches any subset.

Syntax

{
  ...
  "expect": {
    "body": {
      "contains": {
        "key": "value"
      }
    }
  }
}

Example: The api has following response.

{
  "name": "ram",
  "age": 20
}

To test using contains check:

{
  ...
  "expect": {
    "body": {
      "contains": {
        "age": 20
      }
    }
  }
}

The check will pass for the above response as "age": 20 is the subset of response.

3. hasKeys

The hasKeys will check if the provided keys in array are present in the response or not.

Syntax

{
  ...
  "expect": {
    "body": {
      "hasKeys": []
    }
  }
}

Example: The api has following response.

{
  "people": [
    {
      "name": "ram",
      "age": 20
    },
    {
      "name": "Shyam",
      "age": 21
    }
  ]
}

To test using hasKey check:

{
  ...
  "expect": {
    "body": {
      "hasKeys": ["people", "people.0", "people.1", "people.0.name", "people.1.name"]
    }
  }
}

All the above keys are valid in the response. We can compare the key at any depth. While accessing arrays, be sure to use the index without brackets. The key accessing pattern contradicts with the next two checking schemes where bracket is used to access array properties.

4. path_eq

The path_eq does the same check as eq but allows the check to be made inside JSON object path at any depth. The path accessing pattern follows javascript object accessing patterns.

Syntax

{
  ...
  "expect": {
    "path_eq": {
      "path": {"key": "value:"},
      "path.key1.key": 1
    }
  }
}

Example: The api has following response.

{
  "people": [
    {
      "name": "ram",
      "age": 20
    },
    {
      "name": "Shyam",
      "age": 21
    }
  ]
}

To test using path_eq check:

{
  ...
  "expect": {
    "body": {
      "path_eq": {
        "people[0]": {
          "name": "ram",
          "age": 20
        },
        "people[1].name": "Shyam" 
      }
    }
  }
}

The above example shows how to access an object path to compare and check the values at any depths.

5. path_contains

The path_contains does the same check as contains but allows the check to be made inside JSON object path at any depth. The path accessing pattern follows javascript object accessing patterns.

Syntax

{
  ...
  "expect": {
    "path_contains": {
      "path": "value",
      "path.key1.key": "value"
    }
  }
}

Example: The api has following response.

{
  "people": [
    {
      "name": "ram",
      "age": 20
    },
    {
      "name": "Shyam",
      "age": 21
    }
  ]
}

To test using path_contains check:

{
  ...
  "expect": {
    "body": {
      "path_contains": {
        "people[0]": {
          "name": "ram",
        },
        "people[1].name": "Shyam",
        "people": []
      }
    }
  }
}

The above example shows how to access an object path to compare and check the values at any depths. All the above comparison are a subset of response and will pass the check.

v0.1.0

3 years ago