All Projects â†’ subeshb1 â†’ api-test

subeshb1 / api-test

Licence: MIT license
ðŸŒŋ A simple bash script to test JSON API from terminal in a structured and organized way.

Programming Languages

shell
77523 projects
typescript
32286 projects
Dockerfile
14818 projects

Projects that are alternatives of or similar to api-test

karate-runner
VSCode Extension for Karate
Stars: ✭ 23 (-56.6%)
Mutual labels:  testing-tools, api-testing, automated-testing, testing-framework
karate
Test Automation Made Simple
Stars: ✭ 6,384 (+11945.28%)
Mutual labels:  testing-tools, api-testing, automated-testing, testing-framework
Karate
Test Automation Made Simple
Stars: ✭ 5,497 (+10271.7%)
Mutual labels:  testing-tools, api-testing, automated-testing, testing-framework
PixelTest
Fast, modern, simple iOS snapshot testing written purely in Swift.
Stars: ✭ 56 (+5.66%)
Mutual labels:  test, testing-tools, testing-framework
Mockbin
Mock, Test & Track HTTP Requests and Response for Microservices
Stars: ✭ 1,782 (+3262.26%)
Mutual labels:  testing-tools, api-test, api-testing
scalatest-junit-runner
JUnit 5 runner for Scalatest
Stars: ✭ 30 (-43.4%)
Mutual labels:  test, testing-tools, testing-library
kheera-testrunner-android
BDD Framework for Android
Stars: ✭ 18 (-66.04%)
Mutual labels:  integration-testing, testing-tools, testing-framework
Gest
ðŸ‘Ļ‍ðŸ’ŧ A sensible GraphQL testing tool - test your GraphQL schema locally and in the cloud
Stars: ✭ 109 (+105.66%)
Mutual labels:  test, integration-testing, testing-tools
Wasmite
Now WebAssembly has proper testing, unit-testing and debugging ðŸĪ—
Stars: ✭ 20 (-62.26%)
Mutual labels:  test, testing-tools, testing-framework
IO-TESTER
A functional test framework
Stars: ✭ 32 (-39.62%)
Mutual labels:  test, testing-tools, testing-framework
eat
Json based scenario testing tool(which can have test for functional and non-functional)
Stars: ✭ 41 (-22.64%)
Mutual labels:  test, testing-tools, testing-framework
Swagger meqa
Auto generate and run tests using swagger/OpenAPI spec, no coding needed
Stars: ✭ 151 (+184.91%)
Mutual labels:  test, testing-tools, testing-framework
eaf-linter
ðŸĪŠ A linter, prettier, and test suite that does everything as-simple-as-possible.
Stars: ✭ 17 (-67.92%)
Mutual labels:  test, testing-tools, testing-framework
test junkie
Highly configurable testing framework for Python
Stars: ✭ 72 (+35.85%)
Mutual labels:  testing-tools, testing-framework
carina
Carina automation framework: Web, Mobile, API, DB etc testing...
Stars: ✭ 652 (+1130.19%)
Mutual labels:  test, testing-tools
playwright-test
Run unit tests with several runners or benchmark inside real browsers with playwright.
Stars: ✭ 81 (+52.83%)
Mutual labels:  test, testing-tools
ngx-testbedder
CLI tool for writing the test bed for Angular integration test
Stars: ✭ 13 (-75.47%)
Mutual labels:  test, integration-testing
threat9-test-bed
No description or website provided.
Stars: ✭ 26 (-50.94%)
Mutual labels:  test, testing-tools
nim-contra
Lightweight Self-Documenting Design by Contract Programming and Security Hardened mode.
Stars: ✭ 46 (-13.21%)
Mutual labels:  testing-tools, testing-framework
mocat
🐈 Mocat is a mocking toolbar that allows you to interactively develop and test network requests.
Stars: ✭ 27 (-49.06%)
Mutual labels:  testing-tools, testing-library

api-test log

API testing framework for the terminal


api-test is a simple bash script to test JSON API from terminal in a structured and organized way.

CI Release

Docs: https://subeshbhandari.com/api-test

Setting up

Requirements

In cloud 9 / CentOs / aws linux image

sudo yum install jq

Installing

Pull the script

curl -LJO https://raw.githubusercontent.com/subeshb1/api-test/master/api-test.sh

Make the script executable

chmod +x api-test.sh

Move it to /usr/local/bin to make it executable from anywhere

sudo mv api-test.sh /usr/local/bin/api-test

Check if the installation is successful

api-test --help

Alternate Approach

Since it is a small bash file, you can copy the content in https://raw.githubusercontent.com/subeshb1/api-test/master/api-test.sh and paste in a file, make it executable and run it.

Usage

$ api-test.sh -h

A simple program to test JSON APIs.

USAGE: api-test [-hv] -f file_name [CMD] [ARGS]

OPTIONS:
  -h (--help)       print this message
  -v (--verbose)    verbose logging
  -f (--file)       file to test
  --version         print the version of the program

COMMANDS:
  run               Run test cases specified in the test file.
  test              Run automated test in the test file.
  describe          List test cases or describe the contents in a test case.

Run 'api-test COMMAND --help' for more information on a command.

Test file

The test file will contain test cases in json format.

Example: test.json

{
  "name": "My API test",
  "testCases": {
    "test_case_1": {
      "path": "/path_1",
      "method": "POST",
      "description": "Best POST api",
      "body": {
        "value": 1
      },
      "header": {
        "X-per": "1"
      }
    },
    "test_case_2": {
      "path": "/path_2",
      "method": "GET",
      "description": "Best GET api",
      "query": {
        "value": 1
      }
    },
    "test_case_3": {
      "path": "/path_1",
      "method": "DELETE",
      "description": "Best DELETE api",
      "body": {
        "value": 1
      }
    }
  },
  "url": "localhost:3000",
  "header": {
    "Authorization": "Bearer <ACCESS_TOKEN>"
  }
}

The test cases are present in the testCases object. The main url for the api is store in url string. If the test cases share common headers add them in root header key.

To pull the template.json

curl -LJO https://raw.githubusercontent.com/subeshb1/api-test/master/template.json

Running test case

api-test -f test.json run test_case_1 # running single test case
api-test -f test.json run test_case_1 test_case_2 # running multiple test case
api-test -f test.json run all # running all test case. WARNING: Don't name a test case `all`
api-test -v -f test.json run test_case_1 # To run in verbose mode use `-v`

Automated testing

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": {
     "body": {
      "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": []
      }
    }
  }
}

6. External scripts or program

If none of the above checks work for you, there is a way to inject any language to compare and test an api response. To do so, provide the command name or script in the external key in the expect block. If a test case passes return an exit code 0 and if a test fails exit code > 0 to communicate with the api-test program.

Syntax

{
  ...
  "expect": {
    "body": {...},
    "header": {...},
    "external": "<your program>"
  }
}

Example:

test.json

{
  ...
  "expect": {
    "body": {...},
    "header": {...},
    "external": "node test.js"
  }
}

test.js

let testCase = process.argv[2]; // First arg will be test case key
let body = process.argv[3]; // Second arg will be body
let header = process.argv[4]; // Third arg will be header

let success = true;
switch (testCase) {
  case "get_api":
    if (success) {
      process.exit(0); // For success case
    } else {
      process.exit(1); // For failure case
    }
    break;
  case "invalid_post_api":
    ...
    break;

  default:
    break;
}

The test case key, body and header are passed respectively to the supplied program. You can use any language as long as you are sending out the correct exit code for failure and success.

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.

Uninstalling

rm /usr/local/bin/api-test
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].