ตรวจ JSON Response สำหรับ API Testing
ตัวอย่างแยก JSON Syntax, Schema และ Business Rule พร้อม JSONPath
สมมติ GET /orders ส่ง {"orders":[{"id":"T-01","total":120,"status":"paid"}],"count":1} โดย count ต้องเท่ากับจำนวนรายการ และ total เป็น number.
ตัวอย่างชุดทดสอบ
| สถานการณ์ | ข้อมูล / วิธีทดสอบ | ผลที่คาดหวัง |
|---|---|---|
| Syntax | JSON ที่มี comma ท้าย | ต้อง parse ไม่ได้ |
| Schema | total เป็น "120" | JSON ถูก แต่ type ไม่ตรง |
| Business | count เป็น 2 แต่มี 1 รายการ | ไม่ผ่าน business rule |
| JSONPath | $.orders[*].status | พบ paid |
| Missing vs null | ลบ total เทียบ total:null | ตรวจแยกตาม schema |
| HTTP status | 401/403 | ไม่แสดงข้อมูลออเดอร์ที่ไม่มีสิทธิ์ |
วิธีนำไปใช้กับงาน QA
ใช้ Formatter ตรวจ syntax แล้วใช้ JSONPath หาค่าเพื่อเขียน assertion ใน Postman หรือเครื่องมือ API ของทีม. ตรวจ status code, content type, schema และสิทธิ์แยกกัน. ลบ token และข้อมูลลูกค้าก่อนคัดลอก response.