Skip to content

Latest commit

 

History

History
363 lines (264 loc) · 5.01 KB

slides.md

File metadata and controls

363 lines (264 loc) · 5.01 KB
layout lineNumbers class
center
true
text-center


layout: center

what is pactum

An open-source library for testing RESTful APIs written in javascript for all levels in a Testing Pyramid.


layout: center

setup & installation

npm install pactum <test-runner>

layout: center

clear & comprehensive


layout: center

await spec().<command>().<command>()       

simple test

a simple test case using PactumJS and mocha.




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

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

advanced test




const { spec } = require('pactum');
const { like } = require('pactum-matchers');

it('should create a new user', async () => {
  await spec()
    .post('https://reqres.in/api/users')
    .withJson({
      "name": "morpheus",
      "job": "leader"
    })
    .expectStatus(201)
    .expectJsonMatch('id', like("1"));
});

layout: center

data management

  • templates
  • maps
  • functions
  • stores

adding a data template



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

stash.addDataTemplate({
  'USER_DTO': {
    "name": "morpheus",
    "job": "leader"
  }
});

using a data template


await spec()
  .post('https://reqres.in/api/users')
  .withJson({
    "@DATA:TEMPLATE@": "USER_DTO"
  });


await spec()
  .post('https://reqres.in/api/users')
  .withJson({
    '@DATA:TEMPLATE@': 'User',
    '@OVERRIDES@': {
      'job': 'member'
    }
  });

layout: center

handlers

  • spec
  • expect
  • interaction

adding a spec handler



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

handler.addSpecHandler('get random users', (ctx) => {
  const { spec, data } = ctx;
  spec.get('https://randomuser.me/api');
  spec.withQueryParams('results', data);
  spec.expectStatus(200);
});

using a spec handler



await spec('get random users', 2)
  .expectJsonLike({
    "results": "$V.length === 2"
  });

await spec('get random users', 3)
  .expectJsonLike({
    "results": "$V.length === 3"
  });

layout: center

integration testing


stores


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

it('should return all posts and first post should have comments', async () => {
  await spec()
    .get('http://jsonplaceholder.typicode.com/posts')
    .expectStatus(200)
    .stores('FirstPostId', '[0].id');
  
  await spec()
    .get(`http://jsonplaceholder.typicode.com/posts/{id}/comments`)
    .withPathParams('id', '$S{FirstPostId}')
    .expectStatus(200);
});

layout: center

mock server


adding an interaction

interactions will help to simulate the HTTP requests & responses


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

mock.addInteraction({
  request: {
    method: 'GET',
    path: '/api/hello'
  },
  response: {
    status: 200,
    body: 'Hello, 👋'
  }
});

mock.start(3000);

layout: center

component testing


layout: fact

graph LR;
    subgraph Controlled Environment
    t[Tests]
    sut(Service Under Test)
    ms(Mock Server)
    db[(Database)]

    t-- Request -->sut
    sut-- Read/Write -->db
    sut-- Request -->ms
    ms-- Response -->sut
    sut-- Response -->t
    end
Loading

layout: fact

graph LR;
    subgraph Workflow
    u{{User}}
    os(Order Service)
    is(Inventory Server)

    u-- Place Order -->os
    os-- Check Inventory -->is
    end
Loading

component tests


it('should buy a product which is in-stock', async () => {
  await pactum.spec()
    .useInteraction('get in-stock product from inventory-service')
    .post('/api/order-service/v1/orders')
    .withJson({
      "product": "iPhone"
    })
    .expectStatus(200);
});

it('should not buy a product which is out-of-stock', async () => {
  await pactum.spec()
    .useInteraction('get out-of-stock product from inventory-service')
    .post('/api/order-service/v1/orders')
    .withJson({
      "product": "iPhone"
    })
    .expectStatus(400);
});

layout: center class: text-center

Learn More

Documentation · GitHub