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);
    });
});
相关推荐
niuniu_6666 小时前
简单的自动化场景(以 Chrome 浏览器 为例)
运维·chrome·python·selenium·测试工具·自动化·安全性测试
niuniu_6661 天前
Selenium 性能测试指南
selenium·测试工具·单元测试·测试·安全性测试
莓事哒1 天前
selenium和pytessarct提取古诗文网的验证码(python爬虫)
爬虫·python·selenium·测试工具·pycharm
软件测试曦曦1 天前
如何使用Python自动化测试工具Selenium进行网页自动化?
自动化测试·软件测试·python·功能测试·测试工具·程序人生·自动化
互联网杂货铺1 天前
黑盒测试、白盒测试、集成测试和系统测试的区别与联系
自动化测试·软件测试·python·功能测试·测试工具·单元测试·集成测试
Feng.Lee2 天前
聊一聊缓存如何进行测试
功能测试·测试工具·缓存
爱学测试的雨果2 天前
Postman —— postman实现参数化
软件测试·功能测试·测试工具·lua·postman
互联网杂货铺2 天前
如何用Postman实现自动化测试?
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
niuniu_6662 天前
安全性测试(Security Testing)
测试工具·单元测试·appium·测试·安全性测试
薄荷你玩_2 天前
简单粗暴,用浏览器调试端口绕过Selenium/Playwright/Puppeteer检测
selenium·测试工具