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:
- Hapus seluruh tab yang
telah dibuat sebelumnya
- Pilih tab Collection >
klik tombol + yang ada di sebelahnya
- Ubah nama New
Collection dengan Notes API Test
- Pilih tab Environment >
Create a new Environment
- Ubah nama New
Environment dengan Notes API Test
- Tambah variabel baru
dengan nama noteId
- Klik tombol save
- Collection dan Environment untuk pengujian
otomatis sudah siap!
Setelah collection dan environment siap maka akan dilanjutkan
pembuatan skenario pertama, ikuti langkah di bawah:
Buka tab collection
- Klik Add a request dan
beri nama Adding Notes
- Isi request URL
dengan localhost:5000/notes dan gunakan method POST
- Klik opsi Body > raw
kemudian ganti text menjadi JSON
- Pada request gunakan
kode:
{
- "title":
"Catatan A",
- "tags":
["Android", "Web"],
- "body":
"Isi dari catatan A"
- }
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);
- });
- Lihat di bagian
response pada tab Test Results
- Akan ada
error AssertionError: expected 'application/json;
charset=utf-8' to equal 'application/json'
- 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');
- });
- Seluruh pengujian berhasil!
- Pastikan variabel noteId pada environment sudah terisi nilai, jika belum perhatikan set environment pada bagian kanan atas apakah masih No Environment
- Skenario pertama berhasil (jangan lupa save)!
- Buat request baru pada collection Notes API Test dengan nama Getting All Notes
- Isi request URL dengan localhost:5000/notes dan gunakan method GET
- 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);
- });
- Klik tombol Send
- Skenario kedua berhasil (jangan lupa di save)!
- Buat request baru di Notes API Test dengan nama Getting Specified Note
- Isi request URL dengan localhost:5000/notes/{{noteId}} dan gunakan method GET
- 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);
- });
- Klik tombol Send
- Akan muncul error AssertionError: expected [ 'Android', 'Web' ] to equal [ 'Android', 'Web' ]
- 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);
- });
- Klik tombol Send
- Permintaan secara spesifik berhasil di uji!
- Buat request di collection Notes API Test dengan nama Update Note
- Pada request URL isi dengan localhost:5000/notes/{{noteId}} dan gunakan method PUT
- 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"
- }
- 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);
- });
- }
- });
- });
- Klik tombol Send
- Seluruh pengujian berhasil dilakukan (jangan lupa di save)!
Sudah 4 skenario di uji dan skenario terakhir adalah pengujian menghapus note, ikuti langkah di bawah:
- Buat request baru dalam Collection Notes API Test dengan nama Delete Note
- Isi request URL dengan nilai localhost:5000/notes/{{noteId}} dan gunakan method DELETE
- 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');
- });
- }
- });
- });
- Klik tombol Send
- Pengujian menghapus note berhasil (jangan lupa di save)!
- Klik collection Notes API Test
- Klik tombol Run
- Pasitkan urutannya telah benar kemudian klik tombol Run Notes API Test
- 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