如何在JSON元素中定义内部依赖关系?

2024-05-23 22:41:28 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有以下定义API的JSON:

{"contract_name": "create_user",
        "method": "POST",
        "url": "/users",
        "request_body": {
            "username": {
                "type": "email",
                "options": "None",
                "required": "true",
                "unique":"true"
            },
            "role": {
                "type": "string",
                "options": [
                    "TESTER",
                    "RA_DEVICE"
                ],
                "required": "true"
            },
            "userProfile": {
                "type": "object",
                "options": {
                    "TESTER": {
                        "firstName": {
                            "type": "string",
                            "options": "None",
                            "required": "true"
                        },
                        "lastName": {
                            "type": "string",
                            "options": "None",
                            "required": "true"
                        }
                    },
                    "RA_DEVICE": {
                        "deviceId": {
                            "type": "long",
                            "options": "None",
                            "required": "true",
                            "unique":"true"
                        }
                    }}
}

现在让我们假设我想从泛型python脚本创建API测试用例(它可以接受任何API定义,并使用元素的一些“type”随机值为其生成测试用例)

问题:我想在“userProfile”和“role”之间定义一个条件,以便“userProfile”值应该基于“role”的值。 i、 e.如果我的python脚本选择“rau DEVICE”作为角色,那么
“userProfile”应该对应于选项中的“rau DEVICE”,它是一个表示“deviceId”的随机数

请建议一种方法来定义“role”和“userProfile”之间的关系,考虑到我生成测试用例的python脚本是通用的(可以为任何具有元素内依赖关系的API生成测试用例)


Tags: 脚本noneapitruestring定义devicetype