https://medium.com/@naruapon/การเขียน-test-script-ใน-postman-dbbbafd56518

สร้าง Collection ใหม่ และตั้งชื่อ Collection (User API Tests)

สร้าง Request ใหม่ใน Collection

• ใน Collection คลิกที่ปุ่ม Add Request

• ตั้งชื่อ Request (เช่น Create User)

กำหนด Endpoint และ Method

• กำหนด URL เป็น http://127.0.0.1:5000/users

• เลือก Method เป็น POST

• ในแท็บ Body เลือก raw และตั้งค่าเป็น JSON

• ใส่ข้อมูล JSON ในการสร้างผู้ใช้ใหม่

{
    "name": "John Doe",
    "email": "john@example.com"
}

เขียน Test Script สำหรับ Request

• ไปที่แท็บ Tests

• เขียน test script

pm.test("Status code is 201", function () {
    pm.response.to.have.status(201);
});

pm.test("Response has user ID", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('id');
});

pm.test("Response has user data", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.user).to.have.property('name', 'John Doe');
    pm.expect(jsonData.user).to.have.property('email', 'john@example.com');
});

สร้าง Request สำหรับการเรียกดูข้อมูลผู้ใช้

• สร้าง Request ใหม่ใน Collection (Get User)

• กำหนด URL เป็น http://127.0.0.1:5000/users/:id (แทนที่ :id ด้วย user ID ที่ได้จากการสร้างผู้ใช้)

• เลือก Method เป็น GET

• ในแท็บ Tests เขียน test script

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.test("Response has user ID", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('id');
});

pm.test("Response has correct user data", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.user).to.have.property('name', 'John Doe');
    pm.expect(jsonData.user).to.have.property('email', 'john@example.com');
});

สร้าง Request สำหรับการลบผู้ใช้

• สร้าง Request ใหม่ใน Collection (Delete User)

• กำหนด URL เป็น http://127.0.0.1:5000/users/:id (แทนที่ :id ด้วย user ID ที่ได้จากการสร้างผู้ใช้)

• เลือก Method เป็น DELETE

• ในแท็บ Tests เขียน test script

pm.test("Status code is 204", function () {
    pm.response.to.have.status(204);
});

รัน Collection ทั้งหมด

• ไปที่ Collection

• คลิกที่ปุ่ม Run

• เลือก Requests ที่ต้องการรัน

• คลิก Run เพื่อรันทั้งหมด