状态码验证
javascript
// 验证状态码=200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
响应时间验证
javascript
// 验证响应时间<500ms
pm.test("Response time is within acceptable range", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});
响应主体验证
javascript
// 验证响应body包含预期值
pm.test("Response body contains expected data", function () {
pm.expect(pm.response.text()).to.include("expected_value");
});
JSON响应验证
javascript
// 验证响应是否有效json
pm.test("Response is valid JSON", function () {
pm.expect(pm.response.json()).to.be.an("object");
});
请求头验证
javascript
// 验证请求包含请求头
pm.test("Header is present in the response", function () {
pm.response.to.have.header("header_name");
});
链接测试
javascript
pm.test("Chaining multiple assertions", function () {
pm.expect(pm.response.json().property1).to.eql("value1");
pm.expect(pm.response.json().property2).to.eql("value2");
});
动态数据验证
javascript
pm.test("Dynamic data validation", function () {
pm.expect(pm.response.json().user_id).to.eql(pm.environment.get("user_id"));
});
从环境变量设置数据
javascript
var jsonData = pm.response.json();
pm.environment.set("companyIdToDelete", jsonData.data._id);
响应结构验证
javascript
pm.test("Response schema validation", function () {
const schema = {
"type": "object",
"properties": {
"property1": { "type": "string" },
"property2": { "type": "number" }
}
};
pm.expect(tv4.validate(pm.response.json(), schema)).to.be.true;
});
动态响应验证
javascript
pm.test("Dynamic response validation", function () {
var responseBody = pm.response.json();
var dynamicValue = responseBody.dynamic_property;
pm.expect(dynamicValue).to.equal(pm.variables.get("expected_dynamic_value"));
});
API版本验证
javascript
var apiVersion = "v2";
pm.test("API versioning validation", function () {
pm.request.headers.add({ key: "Accept-Version", value: apiVersion });
pm.sendRequest(function (response) {
pm.expect(response.status).to.equal(200);
pm.expect(response.json().version).to.equal(apiVersion);
});
});
文件上传
javascript
// Test file upload
pm.test("File upload validation", function () {
var formData = {
file: {
value: pm.globals.get("fileData"),
options: {
filename: "sample.txt",
contentType: "text/plain"
}
}
};
pm.sendRequest({
url: "https://api.example.com/upload",
method: "POST",
body: formData
}, function (response) {
pm.expect(response.status).to.equal(200);
pm.expect(response.json().success).to.be.true;
});
});
身份和授权验证
javascript
pm.test("Authentication and authorization", function () {
var authToken = pm.environment.get("authToken");
pm.request.headers.add({ key: "Authorization", value: "Bearer " + authToken });
pm.sendRequest(function (response) {
pm.expect(response.status).to.equal(200);
pm.expect(response.json().authorized).to.be.true;
});
});
数据提取和转换
javascript
pm.test("Data extraction and transformation", function () {
var responseArray = pm.response.json().data;
var transformedData = responseArray.map(function (item) {
return {
id: item.id,
name: item.first_name + " " + item.last_name"
};
});
pm.expect(transformedData[0].name).to.include("John");
pm.environment.set("transformedData", JSON.stringify(transformedData));
});
会话管理
javascript
var sessionToken;
pm.test("Login and session management", function () {
// 提取登录会话token
sessionToken = pm.response.json().session_token;
pm.environment.set("sessionToken", sessionToken);
});
pm.test("Authenticated request using session token", function () {
// 使用保存的会话请求
pm.request.headers.add({ key: "Authorization", value: "Bearer " + sessionToken });
pm.sendRequest(function (response) {
pm.expect(response.status).to.equal(200);
});
});
分页测试
javascript
pm.test("Pagination testing", function () {
var response = pm.response.json();
var currentPage = response.page;
var totalPages = response.total_pages;
pm.expect(currentPage).to.be.at.least(1);
pm.expect(currentPage).to.be.at.most(totalPages);
});
负载测试
javascript
var concurrentRequests = 10;
pm.test("Load testing", function () {
var requests = [];
for (var i = 0; i < concurrentRequests; i++) {
requests.push(pm.sendRequest);
}
Promise.all(requests).then(function (responses) {
responses.forEach(function (response) {
pm.expect(response.status).to.equal(200);
});
});
});
数据库验证
javascript
var mockDatabase = [
{ id: 1, name: "Item 1" },
{ id: 2, name: "Item 2" }
];
pm.test("Database validation", function () {
var response = pm.response.json();
mockDatabase.forEach(function (item) {
var matchingItem = response.find(function (responseItem) {
return responseItem.id === item.id;
});
pm.expect(matchingItem.name).to.equal(item.name);
});
});