Postman常用测试脚本

状态码验证

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);
    });
});
相关推荐
cmgdxrz9 小时前
Postman接口测试02|执行接口测试、全局变量和环境变量、接口关联、动态参数、断言
测试工具·postman
测试杂货铺10 小时前
UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·测试用例·pytest
m0_7482411210 小时前
Selenium之Web元素定位
前端·selenium·测试工具
柠檬不萌只是酸i13 小时前
day19——web自动化测试(1)
selenium·测试工具
互联网杂货铺15 小时前
单元测试/系统测试/集成测试知识总结
自动化测试·软件测试·测试工具·职场和发展·单元测试·测试用例·集成测试
梦游钓鱼15 小时前
Peach模糊测试工具
测试工具
一条测试老狗21 小时前
【可视化开源性能压测工具】小巧而强大的oha
测试工具·性能压测·oha
测试者家园1 天前
ChatGPT与Postman协作完成接口测试(一)
软件测试·chatgpt·接口测试·postman·用chatgpt做软件测试·测试图书·质量效能