Trong bài trước chúng ta đã tìm hiểu căn bản về viết Tests trong Postman, trong bài này chúng ta sẽ tìm hiểu thêm các đoạn code khác để kiểm tra nhiều hơn như response body, content type…
1. Kiểm tra response code = 200
1 2 3 |
pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); |
2. Kiểm tra status code name là “OK”
1 2 3 |
pm.test("Status code name has string OK", function () { pm.response.to.have.status("OK"); }); |
3. Kiểm tra thời gian response trả về thấp hơn 500ms
1 2 3 |
pm.test("Response time is less than 500ms", function () { pm.expect(pm.response.responseTime).to.be.below(500); }); |
4. Kiểm tra response code trả về là 200, 201 hoặc 202
1 2 3 |
pm.test("Successful POST request", function () { pm.expect(pm.response.code).to.be.oneOf([200,201,202]); }); |
5. Response có chứa Content-Type
1 2 3 |
pm.test("Content-Type is present", function () { pm.response.to.have.header("Content-Type"); }); |
6. Response body có chứa một nội dung mong muốn
1 2 3 |
pm.test("Body matches string", function () { pm.expect(pm.response.text()).to.include("Employee added successfully!"); }); |
One Comment