Menguji RESTful API Dengan Postman

Untuk melakukan pengujian secara
otomatis dengan postman akan digunakan 2 fitur, yaitu collection dan
environment. Fitur collection berisi kumpulan request yang pernah
dilakukan. Kumpulan request yang pernah dilakukan terkumpul pada
history, namun jika request yang dilakukan begitu banyak maka akan
butuh waktu untuk memilih request yang akan digunakan ulang. Oleh
sebab itu ada fitur collection yang digunakan untuk mengelompokkan
dan mengurutkan request kemudian dapat dijalankan secara berurut
melalui tombol run.

Sedangkan fitur environment merupakan sekumpulan dari
variable yang dapat digunakan pada request. Maksudnya terkadang ada
nilai yang perlu disimpan selama pengujian, nah, nilai tersebut di
simpan dalam sebuah atau beberapa variabel yang mana variabel
tersebut ada di dalam fitur environment. Untuk menggunakan variabel
pada environment gunakan syntax seperti {{noteId}}.
Sekarang akan dibuat sebuah pengujian otomatis dengan skenario di
bawah:


  • Memasukkan catatan baru



    • Response status code
      201
    • Header response
      Content-Type memiliki nilai application/json
    • Body response adalah
      object

    • Body response memiliki
      properti dan nilai yang sesuai

    • Data pada response
      body memiliki noteId dan nilainya tidak kosong


  • Mendapatkan seluruh
    catatan



    • Response status code
      200

    • Header response
      Content-Type memiliki nilai application/json

    • Body response adalah
      object

    • Body response memiliki
      properti dan nilai atau tipe data yang sesuai

    • Data pada response
      body memiliki array notes dan terdapat minimal 1 item di dalamnya


  • Mendapatkan catatan
    secara spesifik



    • Response status code
      200

    • Header response
      Content-Type memiliki nilai application/json

    • Body response
      merupakan object

    • Body response memiliki
      properti dan nilai atau tipe data yang sesuai

    • Data pada response
      body memiliki properti note yang merupakan sebuah objek

    • Objek note di dalam
      data memiliki properti id, title, body, dan tags  dengan nilai
      yang sesuai


  • Memperbarui data
    catatan



    • Response status code
      200

    • Header response
      Content-Type memiliki nilai application/json

    • Body response adalah
      object

    • Body response memiliki
      properti dan nilai yang sesuai

    • Ketika mengakses
      catatan yang diperbaharui


      • Pastikan catatan yang
        diperbarui memiliki nilai terbaru



  • Menghapus catatan



    • Response status code
      200

    • Header response
      Content-Type memiliki nilai application/json

    • Body response adalah
      object

    • Body response memiliki
      properti dan nilai yang sesuai

    • Ketika mengakses
      catatan yang dihapus


      • Pastikan catatan yang dihapus tidak ditemukan



Sekarang akan diterapkan skenario-skenario tersebut, ikuti langkah
di bawah:


  1. Hapus seluruh tab yang
    telah dibuat sebelumnya

  2. Pilih tab Collection >
    klik tombol + yang ada di sebelahnya

  3. Ubah nama New
    Collection dengan Notes API Test

  4. Pilih tab Environment >
    Create a new Environment

  5. Ubah nama New
    Environment dengan Notes API Test

  6. Tambah variabel baru
    dengan nama noteId

  7. Klik tombol save

  8. Collection dan Environment untuk pengujian
    otomatis sudah siap!

Setelah collection dan environment siap maka akan dilanjutkan
pembuatan skenario pertama, ikuti langkah di bawah:


  1. Buka tab collection


  2. Klik Add a request dan
    beri nama Adding Notes

  3. Isi request URL
    dengan localhost:5000/notes dan gunakan method POST

  4. Klik opsi Body > raw
    kemudian ganti text menjadi JSON

  5. Pada request gunakan
    kode:


    • {


    •    "title":
      "Catatan A",

    •    "tags":
      ["Android", "Web"],

    •    "body":
      "Isi dari catatan A"



  6. Pilih tab Test dan
    gunakan kode:



    • pm.test('response
      status code should have 201 value', () => {


    •  
       pm.response.to.have.status(201);

    • }); 


    • pm.test('response
      Content-Type header should have application/json value', () => {

    •  
      pm.expect(pm.response.headers.get('Content-Type')).to.equals('application/json');

    • });


    • pm.test('response
      body should an object', () => {

    •     const
      responseJson = pm.response.json();

    •    
      pm.expect(responseJson).to.be.an('object');

    • }); 


    • pm.test('response
      body should have correct property and value', () => {

    •     const
      responseJson = pm.response.json();

    •    
      pm.expect(responseJson).to.ownProperty('status');

    •    
      pm.expect(responseJson.status).to.equals('success');

    •    
      pm.expect(responseJson).to.ownProperty('message');

    •    
      pm.expect(responseJson.message).to.equals('Catatan
      berhasil ditambahkan');

    •    
      pm.expect(responseJson).to.ownProperty('data');

    •    
      pm.expect(responseJson.data).to.be.an('object');

    • });


    • pm.test('response
      body data should have noteId property and not equal to empty', ()
      => {

    •     const
      responseJson = pm.response.json();

    •     const
      { data } = responseJson;

    •  

    •    
      pm.expect(data).to.ownProperty('noteId');

    •    
      pm.expect(data.noteId).to.not.equals('');

    •  

    •    
      pm.environment.set('noteId',
      data.noteId);

    • });


  7. Lihat di bagian
    response pada tab Test Results

  8. Akan ada
    error AssertionError: expected 'application/json;
    charset=utf-8' to equal 'application/json'

  9. Solusinya edit kode
    dengan:


    • pm.test('response
      Content-Type header should have application/json value', () => {


    •  
      pm.expect(pm.response.headers.get('Content-Type')).to.equals('application/json;
      charset=utf-8');

    • }); 

  10. Seluruh pengujian berhasil!
  11. Pastikan variabel noteId pada environment sudah terisi nilai, jika belum perhatikan set environment pada bagian kanan atas apakah masih No Environment
  12. Skenario pertama berhasil (jangan lupa save)!
Sekarang akan dilanjutkan skenario kedua, ikuti langkah di bawah:
  1. Buat request baru pada collection Notes API Test dengan nama Getting All Notes
  2. Isi request URL dengan localhost:5000/notes dan gunakan method GET
  3. Pilih tab Tests dan gunakan kode di bawah:
    • pm.test('response status code should have 200 value', () => {
    •    pm.response.to.have.status(200);
    • });

    • pm.test('response Content-Type header should have application/json value', () => {
    •    pm.expect(pm.response.headers.get('Content-Type')).to.equals('application/json; charset=utf-8');
    • }); 

    • pm.test('response body should an object', () => {
    •    const responseJson = pm.response.json();
    •    pm.expect(responseJson).to.be.an('object');
    • }); 

    • pm.test('response body should have the correct property and value', () => {
    •   const responseJson = pm.response.json();
    •    pm.expect(responseJson).to.have.ownProperty('status');
    •    pm.expect(responseJson.status).to.equals('success');
    •    pm.expect(responseJson).to.have.ownProperty('data');
    •    pm.expect(responseJson.data).to.be.an('object');
    • }); 

    • pm.test('response body data should have a notes array and contain at least 1 item', () => {
    •    const responseJson = pm.response.json();
    •    const { data } = responseJson;
    •  
    •    pm.expect(data).to.have.ownProperty('notes');
    •    pm.expect(data.notes).to.be.an('array');
    •    pm.expect(data.notes).lengthOf.at.least(1);
    • }); 
  4. Klik tombol Send
  5. Skenario kedua berhasil (jangan lupa di save)!
Sekarang akan dilakukan skenario ketiga, yaitu mendapatkan catatan spesifik, ikuti langkah di bawah:
  1. Buat request baru di Notes API Test dengan nama Getting Specified Note
  2. Isi request URL dengan localhost:5000/notes/{{noteId}} dan gunakan method GET
  3. Kemudian ke tab Test dan gunakan kode di bawah:
    • pm.test('response status code should have 200 value', () => {
    •   pm.response.to.have.status(200);
    • }); 

    • pm.test('response Content-Type header should have application/json value', () => {
    •    pm.expect(pm.response.headers.get('Content-Type')).to.equals('application/json; charset=utf-8');
    • });

    • pm.test('response body should be an object', () => {
    •    const responseJson = pm.response.json();
    •    pm.expect(responseJson).to.be.an('object');
    • });

    • pm.test('response body should have the correct property and value', () => {
    •    const responseJson = pm.response.json();
    •  
    •    pm.expect(responseJson).to.have.ownProperty('status');
    •    pm.expect(responseJson.status).to.equals('success');
    •    pm.expect(responseJson).to.have.ownProperty('data');
    •    pm.expect(responseJson.data).to.be.an('object');
    • }); 

    • pm.test('response body data should contain note object', () => {
    •    const responseJson = pm.response.json();
    •    const { data } = responseJson;
    •  
    •    pm.expect(data).to.have.ownProperty('note');
    •    pm.expect(data.note).to.be.an('object');
    • }); 

    • pm.test('note object should contain correct value for id, title, body, and tags property', () => {
    •    const responseJson = pm.response.json();
    •    const { data: { note } } = responseJson;
    •    const expectedId = pm.environment.get('noteId');
    •    const expectedTitle = 'Catatan A';
    •    const expectedTags = ['Android', 'Web'];
    •    const expectedBody = 'Isi dari catatan A';
    •    pm.expect(note).to.have.ownProperty('id');
    •    pm.expect(note.id).to.equals(expectedId);
    •    pm.expect(note).to.have.ownProperty('title');
    •    pm.expect(note.title).to.equals(expectedTitle);
    •    pm.expect(note).to.have.ownProperty('tags');
    •    pm.expect(note.tags).to.equals(expectedTags);
    •    pm.expect(note).to.have.ownProperty('body');
    •    pm.expect(note.body).to.equals(expectedBody);
    • });
  4. Klik tombol Send
  5. Akan muncul error AssertionError: expected [ 'Android', 'Web' ] to equal [ 'Android', 'Web' ]
  6. Sebagai solusi perhatikan dan gunakan potongan kode di bawah:
    • pm.test('note object should contain correct value for id, title, body, and tags property', () => {
    •    const responseJson = pm.response.json();
    •    const { data: { note } } = responseJson;
    •  
    •    const expectedId = pm.environment.get('noteId');
    •    const expectedTitle = 'Catatan A';
    •    const expectedTags = ['Android', 'Web'];
    •    const expectedBody = 'Isi dari catatan A';
    •  
    •    pm.expect(note).to.have.ownProperty('id');
    •    pm.expect(note.id).to.equals(expectedId);
    •  
    •    pm.expect(note).to.have.ownProperty('title');
    •    pm.expect(note.title).to.equals(expectedTitle);
    •  
    •    pm.expect(note).to.have.ownProperty('tags');
    •    pm.expect(note.tags).to.eql(expectedTags);
    •  
    •    pm.expect(note).to.have.ownProperty('body');
    •    pm.expect(note.body).to.equals(expectedBody);
    • });
  7. Klik tombol Send
  8. Permintaan secara spesifik berhasil di uji!
Sudah 3 skenario yang diuji, yaitu mengirim data, mendapatkan seluruh data, dan mendapatkan data spesifik, sekarang akan dilakukan pengujian update, ikuti langkah di bawah:
  1. Buat request di collection Notes API Test dengan nama Update Note
  2. Pada request URL isi dengan localhost:5000/notes/{{noteId}} dan gunakan method PUT
  3. Pilih tab Body > raw kemudian ubah test menjadi JSON dan gunakan kode di bawah:
    • {
    •     "title": "Catatan A Revised",
    •     "tags": ["Android", "Web"],
    •     "body": "Isi dari Catatan A Revised"
    • }
  4. Pilih tab Test dan gunakan kode di bawah:
    • pm.test('response status code should have 200 value', () => {
    •     pm.response.to.have.status(200);
    • });

    • pm.test('response Content-Type header should have application/json value', () => {
    •    pm.expect(pm.response.headers.get('Content-Type')).to.equals("application/json; charset=utf-8");
    • }); 

    • pm.test('response body should be an object', () => {
    •    const responseJson = pm.response.json();
    •    pm.expect(responseJson).to.be.an('object');
    • });

    • pm.test('response body should have correct property and value', () => {
    •    const responseJson = pm.response.json();
    •  
    •    pm.expect(responseJson).to.have.ownProperty('status');
    •    pm.expect(responseJson.status).to.equals('success');
    •    pm.expect(responseJson).to.have.ownProperty('message');
    •    pm.expect(responseJson.message).to.equals('Catatan berhasil diperbarui');
    • });

    • pm.test('when request the updated note', () => {
    •     const noteId = pm.environment.get('noteId');
    •     pm.sendRequest(`http://localhost:5000/notes/${noteId}`, (error, response) => {
    •         if(!error) {
    •             pm.test('then the updated note should contain the latest data', () => {
    •                 const responseJson = response.json();
    •                 const { data: { note } } = responseJson;
    •  
    •                 const expectedTitle = 'Catatan A Revised';
    •                 const expectedTags = ['Android', 'Web'];
    •                 const expectedBody = 'Isi dari Catatan A Revised';
    •  
    •                 pm.expect(note.title).to.equals(expectedTitle);
    •                 pm.expect(note.tags).to.eql(expectedTags);
    •                 pm.expect(note.body).to.equals(expectedBody);
    •             });
    •         }
    •     });
    • });
  5. Klik tombol Send
  6. Seluruh pengujian berhasil dilakukan (jangan lupa di save)!








Sudah 4 skenario di uji dan skenario terakhir adalah pengujian menghapus note, ikuti langkah di bawah:

  1. Buat request baru dalam Collection Notes API Test dengan nama Delete Note
  2. Isi request URL dengan nilai localhost:5000/notes/{{noteId}} dan gunakan method DELETE
  3. Pili tab Test dan gunakan kode di bawah:
    • pm.test('response status code should have 200 value', () => {
    •    pm.response.to.have.status(200);
    • });

    • pm.test('response Content-Type header should have application/json value', () => {
    •    pm.expect(pm.response.headers.get('Content-Type')).to.equals('application/json; charset=utf-8')
    • }); 

    • pm.test('response body should be an object', () => {
    •    const responseJson = pm.response.json();
    •    pm.expect(responseJson).to.be.an('object');
    • });

    • pm.test('response body should have correct property and value', () => {
    •    const responseJson = pm.response.json();
    •  
    •    pm.expect(responseJson).to.have.ownProperty('status');
    •    pm.expect(responseJson.status).to.equals('success');
    •    pm.expect(responseJson).to.have.ownProperty('message');
    •    pm.expect(responseJson.message).to.equals('Catatan berhasil dihapus');
    • }); 

    • pm.test('when request the deleted note', () => {
    •     const noteId = pm.environment.get('noteId');
    •     pm.sendRequest(`http://localhost:5000/notes/${noteId}`, (error, response) => {
    •         if(!error) {
    •             pm.test('the deleted note should be not found', () => {
    •                 pm.expect(response.code).to.equals(404);
    •                 const responseJson = response.json();
    •                 pm.expect(responseJson.status).to.equals('fail');
    •                 pm.expect(responseJson.message).to.equals('Catatan tidak ditemukan');
    •             });
    •         }
    •     });
    • });
  4. Klik tombol Send
  5. Pengujian menghapus note berhasil (jangan lupa di save)!
Sekarang sudah lengkap ada 5 collection dengan spesifikasi CRUD atau create, read, update, dan delete. Nah, ke-5 collection tersebut akan dijakankan secara berurut dalam satu waktu, caranya:
  1. Klik collection Notes API Test
  2. Klik tombol Run
  3. Pasitkan urutannya telah benar kemudian klik tombol Run Notes API Test
  4. Semua pengujian berhasil!
Sumber: anonim

Postman biasa digunakan oleh seorang back-end developer untuk menguji kode yang dibuat. Dengan postman pengujian bisa dilakukan secara manual dan secara otomatis.



ref:
https://learning.postman.com/docs/getting-started/introduction/

Komentar