【问题标题】:how to test an api with array on postman如何在邮递员上测试带有数组的api
【发布时间】:2019-06-28 17:26:55
【问题描述】:

应用程序中使用的所有 api 都有一个共同的端点:https://m.companyname.com/application/svcwebservice.php

json 有嵌套数组,响应如下所示:

[{
    "usertype": [{
        "id": "1",
        "type": "Service Manager (Admin)",
        "description": "Service Manager (Admin)"
    }, {
        "id": "2",
        "type": "Customer Relations Executive",
        "description": "Customer Relations Executive"
    }, {
        "id": "3",
        "type": "Service Advisor",
        "description": "Service Advisor"
    }, {
        "id": "4",
        "type": "Accountant (Admin)",
        "description": "Accountant (Admin)"
    }, {
        "id": "5",
        "type": "Technician\/Mechanic",
        "description": "Technician\/Mechanic"
    }, {
        "id": "6",
        "type": "Sales Manager (Admin)",
        "description": "Sales Manager (Admin)"
    }, {
        "id": "7",
        "type": "Sales Consultant",
        "description": "Sales Consultant"
    }, {
        "id": "8",
        "type": "Receptionist",
        "description": "Receptionist"
    }, {
        "id": "9",
        "type": "Implant (Admin)",
        "description": "Implant (Admin)"
    }, {
        "id": "10",
        "type": "Vice President \/ General Manager - Sales (Admin)",
        "description": "Vice President \/ General Manager - Sales (Admin)"
    }, {
        "id": "11",
        "type": "Group Manager (Admin)",
        "description": "Group Manager (Admin)"
    }, {
        "id": "13",
        "type": "Coordinator",
        "description": "Coordinator"
    }, {
        "id": "14",
        "type": "Customer Relations Manager (Admin)",
        "description": "Customer Relations Manager (Admin)"
    }, {
        "id": "15",
        "type": "Receptionist",
        "description": "Receptionist"
    }, {
        "id": "16",
        "type": "Vice President \/ General Manager - Service (Admin)",
        "description": "Vice President \/ General Manager - Service (Admin)"
    }, {
        "id": "17",
        "type": "Principal (Admin)",
        "description": "Principal (Admin)"
    }, {
        "id": "18",
        "type": "CEO (Admin)",
        "description": "CEO (Admin)"
    }, {
        "id": "19",
        "type": "COO (Admin)",
        "description": "COO (Admin)"
    }, {
        "id": "20",
        "type": "Insurane Admin",
        "description": "Insurane Admin"
    }]
}]

而payload是:requesttype: "getusertypes"

相同的 api 端点为另一个有效负载提供以下响应,例如:

[{
    "cards": [{
        "average_inv_amount": "26",
        "on_time_checkin": "100.00%",
        "ontime_pickup_percentage": "81.89%",
        "average_distance": "5.5",
        "cust_rating": "4.2",
        "total_vehicle": "4"
    }]
}, {
    "pickup_details": [{
        "name": "Awaiting",
        "value": "0"
    }, {
        "name": "Active Pickup",
        "value": "1"
    }, {
        "name": "Not Checked in",
        "value": "0"
    }, {
        "name": "Checked In",
        "value": "0"
    }, {
        "name": "Picked Up Today",
        "value": "1"
    }]
}, {
    "drop_details": [{
        "name": "Awaiting",
        "value": "0"
    }, {
        "name": "To Service Centre",
        "value": "0"
    }, {
        "name": "To Customer",
        "value": "0"
    }, {
        "name": "Active Dropoff ",
        "value": "0"
    }, {
        "name": "Delivered Today",
        "value": "0"
    }]
}, {
    "notification": [{
        "queue_paused_pickup": "0",
        "queue_paused_dropoff": "0",
        "not_checkedin": "1",
        "queue_cancelled": "2",
        "queue_rescheduled": "0",
        "queue_mishaps": "0"
    }]
}, {
    "prepaid": [{
        "pre_paid": "-250",
        "credit_limit": "10000",
        "show_add_credit": "1",
        "allow_booking": "1"
    }]
}]

等等。

问题:

我应该如何使用邮递员进行测试?

【问题讨论】:

  • 你说的payload是什么意思?请求正文?
  • 我在开发者工具中得到的负载
  • 您能否将其添加到问题中,我不清楚 - 当您在开发人员工具中获得有效负载时。

标签: rest api postman


【解决方案1】:

这取决于您要测试的内容。

首先解析您的响应 var jsonData = pm.response.json();

您可以通过像这样的索引来访问它们

pm.test("Your test name", function() {
    var jsonData = pm.response.json();

    //For Entry 4
    pm.expect(jsonData[0].usertype[3].id).to.eql("4");
    pm.expect(jsonData[0].usertype[3].type).to.eql("Accountant (Admin)");
    pm.expect(jsonData[0].usertype[3].description).to.eql("Accountant (Admin)");
});

或者使用 for 循环对它们进行迭代。但这很大程度上取决于您的测试用例。

【讨论】:

    猜你喜欢
    • 2019-05-14
    • 2021-05-12
    • 2022-10-22
    • 2017-12-17
    • 2022-07-28
    • 2021-01-27
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    相关资源
    最近更新 更多