Code Monkey home page Code Monkey logo

pactum's Introduction

logo

PactumJS

Build Coverage Downloads Size Platform

REST API Testing Tool for all levels in a Test Pyramid


PactumJS Demo


PactumJS is a REST API Testing Tool used to automate e2e, integration, contract & component (or service level) tests. It comes with a powerful mock server which can control the state of external dependencies.

  • โšก Swift
  • ๐ŸŽˆ Lightweight
  • ๐Ÿš€ Simple & Powerful
  • ๐Ÿ› ๏ธ Compelling Mock Server
  • ๐Ÿ’Ž Elegant Data Management
  • ๐Ÿ”ง Extendable & Customizable
  • ๐Ÿ“š Clear & Comprehensive Testing Style
  • ๐Ÿ”— Component, Contract & E2E testing of APIs

----------

Documentation

This readme offers an basic introduction to the library. Head over to the full documentation at https://pactumjs.github.io

Need Help

We use Github Discussions to receive feedback, discuss ideas & answer questions.

Installation

# install pactum as a dev dependency
npm install --save-dev pactum

# install a test runner to run pactum tests
# mocha / jest / cucumber
npm install --save-dev mocha

----------

Usage

pactum can be used for all levels of testing in a test pyramid. It can also act as an standalone mock server to generate contracts for consumer driven contract testing.

API Testing

Tests in pactum are clear and comprehensive. It uses numerous descriptive methods to build your requests and expectations.

Simple Test Cases

Using Mocha

Running simple api test expectations.

const pactum = require('pactum');

it('should be a teapot', async () => {
  await pactum.spec()
    .get('http://httpbin.org/status/418')
    .expectStatus(418);
});

it('should save a new user', async () => {
  await pactum.spec()
    .post('https://jsonplaceholder.typicode.com/users')
    .withHeaders('Authorization', 'Basic xxxx')
    .withJson({
      name: 'bolt',
      email: '[email protected]'
    })
    .expectStatus(200);
});
# mocha is a test framework to execute test cases
mocha /path/to/test

Using Cucumber

See pactum-cucumber-boilerplate for more details on pactum & cucumber integration.

// steps.js
const pactum = require('pactum');
const { Given, When, Then, Before } = require('cucumber');

let spec = pactum.spec();

Before(() => { spec = pactum.spec(); });

Given('I make a GET request to {string}', function (url) {
  spec.get(url);
});

When('I receive a response', async function () {
  await spec.toss();
});

Then('response should have a status {int}', async function (code) {
  spec.response().should.have.status(code);
});
Scenario: Check TeaPot
  Given I make a GET request to "http://httpbin.org/status/418"
  When I receive a response
  Then response should have a status 200

Mock Server

pactum can act as a standalone mock server that allows us to mock any server via HTTP or HTTPS, such as a REST endpoint. Simply it is a simulator for HTTP-based APIs.

Running pactum as a standalone mock server.

const { mock } = require('pactum');

mock.addInteraction({
  request: {
    method: 'GET',
    path: '/api/projects'
  },
  response: {
    status: 200,
    body: [
      {
        id: 'project-id',
        name: 'project-name'
      }
    ]
  }
});

mock.start(3000);

----------

Notes

Inspired from frisby and pact.

Support

Like this project! Star it on Github. Your support means a lot to us.

Contributors

If you've ever wanted to contribute to open source, and a great cause, now is your chance! See the contributing docs for more information

Thanks to all the people who contribute.


pactum's People

Contributors

ankurj00 avatar asaianudeep avatar dependabot[bot] avatar leelaprasadv avatar msharma-tw avatar sudhakotni-git avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.