0.1.9 • Published 1 month ago

@9paradox/apitester v0.1.9

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

apitester

A simple rest api testing package. Crafted with easy to use method call and method chaining, so you can write clean api testing automation scripts in javascript and typescript.

🛠️ This project is still in alpha stage and there might be breaking changes in stable release. 🛠️

Features

  • Easily test entire api workflows.
  • Define testing steps directly in json file.
  • Log request and response.
  • Capture time and performance.
  • Load testcase data.
  • Load request and data templates.
  • Query data easily with jmespath or jsonata.
  • Extend functionality using custom functions.

Table of Contents

Installation

Install into an new or existing javascript/typescript project.

npm i @9paradox/apitester

Usage

  1. Initial step is to create a testcase using one of the following way:
    • apitester.createTestCase function to write testcase through code.
    • apitester.createTestCaseFromJsonFile function to build testcase from json file.
    • apitester.getJsonTestCasesFromFolder function to get all json testcases from a folder.
  2. The testcase can perform multiple steps/actions.
  3. Each step/action requires input data. Some of the steps can use output data from previous step as input.
  4. Finally we call test() function to start executing the testcase, which returns a Promise.
    • In case of apitester.getJsonTestCasesFromFolder, we use apitester.runTestCases(testcases) to execute multiple testcases.

Simple example to get started with apitester.createTestCase.

//simple_test.js
const { apitester } = require('@9paradox/apitester');

const testcase = apitester.createTestCase({
  title: 'verify todos endpoint returns status 200',
});

testcase
  //step 1. get results form /todos
  .get('https://jsonplaceholder.typicode.com/todos/')

  //step 2. verify the status is success
  .pickAndVerify({ query: 'status', expected: 200 })

  //perform the test
  .test()
  .then((testResult) => {
    console.log('Testcase success:- ', testResult.success);
    if (!testResult.success) {
      console.log('Testcase failed:- ', testResult.error);
      console.log('Testcase steps:- ', testResult.steps);
    }
  })
  .catch((err) => console.log(err));

to run the file

node simple_test.js

We can use apitester in any nodejs test project such as jest or xv. There are some more examples in the examples folder. PRs adding more examples are very welcome!

Try online

Check out these demo testcase to get started with apitester.

Method Reference

apitester.createTestCase(..)

Create new testcase.

const testcase = apitester.createTestCase({...}: TestCaseOptions);

TestCaseOptions

ParameterTypeRequiredDescription
titlestringnoTitle for the test case.
dataFilePathstringnoFile path for JSON data key-value file.
stepsStepOptions[]noAdd steps while creating testcase. Check type reference.
logPathstringnoLogging folder path.
logEachStepbooleannoUsed to enable logging each step.
callback(data: CallbackData) => Promise<void>noCallback function called before and after each step.

apitester.createTestCaseFromJsonFile(..)

Create new testcase from json test-case file.

const testcase = apitester.createTestCaseFromJsonFile(
  'path-to-file/test-case.json'
);

Make sure the json test case file follows TestCaseOptions schema.

{
  "title": "running apitester from example-test-case.json file",
  "steps": [
    {
      "action": "get",
      "inputData": "https://jsonplaceholder.typicode.com/todos/"
    },
    {
      "action": "pickAndVerify",
      "inputData": {
        "query": "status",
        "expected": 200,
        "toBe": "=="
      }
    }
  ]
}

apitester.getJsonTestCasesFromFolder(..) and apitester.runTestCases(..)

  • We can test multiple json testcase from a given folder.
  • Testcase file name must end with .test.json, for example simple-testcase.test.json, or else custom file extension list can be provided to apitester.getJsonTestCasesFromFolder(..)
  • apitester.runTestCases(..) returns MultiTestCaseResult.
//example folder with testcase
//  path-to/
//    -- json-testcases/
//        -- my-testcase.test.json
//        -- other_scenario.test.json

const testCases = apitester.getJsonTestCasesFromFolder(
  'path-to/json-testcases'
  //,['.test.json', '.apitester'] //custom file extensions (optional)
);

const multiTestCaseResult = await apitester.runTestCases(testCases);

if (!multiTestCaseResult.success) {
  console.log('Testcases failed: ' + multiTestCaseResult.failedTestCases);
}
  • apitester.runTestCases(testCases, callback) also supports callback (data: TestCaseCallbackData) => Promise<void>.

Quester - vscode extension

Create and run apitester testcases directly in vscode now. Install Quester for vscode from Visual Studio Code marketplace.

Quester - vscode extension

apitester-creator

Now easily create testcases with the online drag-and-drop tool - apitester-creator.

apitester-creator

Export json testcases and run them using apitester.getJsonTestCasesFromFolder(..) and apitester.runTestCases(..).

Action/Step methods

Once a new testcase is created, we can perform multiple steps/actions and finally call test() method to execute the test.

Action (ActionName)Input TypeReturn TypeDescription
getGetOptionsTestCasePerform GET http request.
postPostOptionsTestCasePerform POST http request.
axiosAxiosOptionsTestCasePerform http request based on AxiosRequestConfig.
inputDataanyTestCaseHolds input data for next step.
pickDatastringTestCasePerform json query to pick data from last step.
buildDataBuildDataOptionsTestCaseBuild and merge your data from different steps using json query.
formatDatastringTestCaseRender string template based on input data from last step using Eta.js.
formatTemplateFormatTemplateOptionsTestCaseRender template file based on input data from last step using Eta.js.
pickAndVerifyPickAndVerifyOptionsTestCasePerform json query to pick data from last step and do a test assert.
verifyVerifyOptionsTestCaseTo assert data from last step.
verifyTimeTakenVerifyTimeTakenOptionsTestCaseTo assert time taken for last step.
pickStepnumberTestCaseTo pick output data from specific step. Also supports negative index from current step.
addStepStepOptionsTestCaseAdd a steps from JSON object.
customFnCustomFnOptionsTestCaseRun custom function as a step.
customFnFromCustomFnFromOptionsTestCaseRun custom function from a javascript file as a step.
log-TestCaseLast steps will be logged to a file.
getStepnumberStepReturns the specific step with its input and output data.

Type Reference

StepOptions

Create steps from JSON object.

ParameterTypeRequiredDescription
actionActionNameyesAction name such as get,pickAndVerify etc.
inputDataanyyesInput data for the action. Check method reference.

CallbackData

User provided callback function which is called before and after each step.

ParameterTypeRequiredDescription
typebefore or afteryesUsed to identify if the callback is called before or after.
stepStepyesCurrent step.
stepResultStepResultnoStep success result updated in type 'after'.

TestCaseCallbackData

User provided callback function which is called before and after each testcase running with apitester.runTestCases(...).

ParameterTypeRequiredDescription
typebefore or afteryesUsed to identify if the callback is called before or after.
filePathstringyesCurrent testcase file path.
testCaseResultTestCaseResultnoTestcase success result updated in type 'after'.

GetOptions

Create GetOptions using one of the following type.

TypeDescription
stringString URL.
AxiosRequestConfigAxios Request config object.
undefinedTo pick string URL from last step.

PostOptions

Create PostOptions using one of the following type.

TypeDescription
SimplePostConfigObject with string url and any data.
AxiosRequestConfigAxios Request config object.
undefinedTo pick SimplePostConfig from last step.

BuildDataOptions

Query data form different steps and merge into one object.

ParameterTypeRequiredDescription
queriesQueryData[]yesArray of queries to merge data from multiple steps.

QueryData

ParameterTypeRequiredDescription
stepnumberyesStep number to perform json query on.
querystringyesJson query.
namestringyesField name to store value into.

FormatTemplateOptions

Create FormatTemplateOptions using one of the following type.

TypeDescription
stringTemplate filepath.
{'filePath' : string, 'outputDataFormat' : string or number or 'boolean or object}Template filepath and render output type
undefinedTo take type from last step.

PickAndVerifyOptions

Perform json query to pick data from last step and verify the data against expected data.

ParameterTypeRequiredDescription
querystringyesQuery string to pick data.
expectedanyyesExpected data.
toBeToBenoComparison type.

ToBe

Assertion to perform against expected data. == is default.

equal , == , notEqual , != , greaterThan , > , greaterThanOrEqual , >= , lessThan , < , lessThanOrEqual , <= , in , notIn , contains

VerifyOptions

Create VerifyOptions using one of the following type.

TypeDescription
stringstring data.
{'expected' : any, 'toBe' : ToBe or undefined}Expected data to be verified

VerifyTimeTakenOptions

Verify time taken by last step.

ParameterTypeRequiredDescription
expectednumberyesExpected time.
formatms or syesCompare expected time against ms (milliseconds) or s (seconds) of actual time
toBeToBeyesComparison type.

CustomFnOptions

User provided function to run as a step.

ParameterTypeRequiredDescription
stepTypeStepTypeyesType of step.
fnCustomFunctionyesFilepath of the javascript file.

CustomFunction structure

//custom function input parameter and return type structure
myFunction = async (testCase: TestCase, currentStep: Step, lastStep: Step) => {
	return {
		inputData: any;
		outputData: any;
		verification?: VerificationResult; //required when the step is Verification type
	}
}

CustomFnFromOptions

User provided function from a external file to run as a step. The user function should follow CustomFunction definition.

ParameterTypeRequiredDescription
stepTypeStepTypeyesType of step.
filePathstringyesFilepath of the javascript file.
functionNamestringyesName of the function which is exported from the file.

An example of a custom function from a external javascript file.

//function should follow CustomFunction structure.
async function myCustomFunction(testCase, currentStep, lastStep) {
  var output = testCase.data('delectus_aut_autem');
  output = output.toUpperCase();
  var result = {
    inputData: lastStep.outputData,
    outputData: output,
  };
  return result;
}

//should follow exporting the functionName
module.exports.myCustomFunction = myCustomFunction;

StepType

Used to indicate step operation type.

ValuesDescription
ActionTo denote the step will perform some testcase related action.
VerificationTo denote the step will perform verification or assertion on action/s.
LoggingTo denote the step will perform logging action/s.

VerificationResult

Return type for verification step.

ParameterTypeRequiredDescription
verifiedbooleanyesTo denote if the step has passed the assertion.
actualDataanyyesActual data from the step used for assertion.
messagestringnoAny message or verification error message.

QueryLang

Enum to identify which json query library to use.

Query string starts withValuesDescription
@jmespathjmespathTo denote the query will be performed using jmespath.
@jsonatajsonataTo denote the query will be performed using jsonata. Note: jsonata query are work in progress in current release.

Step

Structure of a step.

ParameterTypeDescription
indexnumberStep number starting from 1.
typeStepTypeType of step.
actionActionNameStep/Action method name.
inputDataanyInput data/parameters provided to the action method.
outputDataanyOutput/return data from the action method.
verifiedOptional<boolean>Indicator if the step has been verified against the assertion.
startedAtstringDate time stamp of starting the step.
endedAtstringDate time stamp of ending the step.
timeTaken{ ms: number; s: number }Time taken to execute the step.

TestCaseResult

Return type for testcase test() method.

ParameterTypeRequiredDescription
successbooleanyesDenotes if the testcase is successful.
totalStepsnumberyesTotal number of steps.
executedStepsnumberyesTotal number of steps are executed.
lastExecutedStepnumberyesStep number of last step that was executed.
totalVerificationStepsnumberyesTotal number of verification type of steps .
executedVerificationStepsnumberyesTotal number of verification type of steps that were executed.
totalSuccessfulVerificationStepsnumberyesTotal number of verification type of steps that were executed and were successful.
lastVerificationStepnumberyesStep number of last verification type of step that was executed.
stepsStep[]yesList of all steps in the testcase.
errorErrornoError object containing error details.

MultiTestCaseResult

Structure of multi testcase execution.

ParameterTypeRequiredDescription
successbooleanyesDenotes if all the testcase are successful.
failedTestCasesnumberyesTotal number of failed testcases.
testCaseResultsTestCaseResult[]yesList of all testcase result.
errorErrornoError object containing error details.

Error

Structure of testcase error.

ParameterTypeRequiredDescription
titlestringyesError title with step number where the error occurred.
messagestringnoMore detail description of the error.
exceptionstringnoException in Json stringified form.
typeerror or exceptionnoDenotes the type of error.

Roadmap

  • Refactor code to support plugin architecture
  • Add more actions
  • Update docs
  • Code refactor
  • Add more examples
  • Add support for XML.

License

MIT

0.1.9

1 month ago

0.1.8

1 month ago

0.1.7

1 month ago

0.1.6

1 month ago

0.1.5

1 month ago

0.1.4

1 month ago

0.1.3

1 month ago

0.1.2

5 months ago

0.1.1

5 months ago

0.1.0

8 months ago

0.0.7

11 months ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

2 years ago

0.0.1

2 years ago