通过code码取json数据可以吗

来源:8-9 项目实战之通过json获取预期结果判断case是否通过

cloudonthesun

2021-07-02

老师的get_result_json方法传入的是“success”或者“error”,但是接口error又存在很多种情况,我可以按照code码取预期的json吗

result.json文件如下:

{

    "/login":[{

        "10000":{

            "status": 0,

            "msg": "success",

            "data": null

        },

        "10001":{

            "status": 10001,

            "msg": "用户名不能为空",

            "data": null

        },

        "10002": {

            "status": 10002,

            "msg": "密码不能为空",

            "data": null

        },

        "10006": {

            "status": 10006,

            "msg": "密码错误",

            "data": null

        }

    

    }],

    "/register": [{

        "10000": {

            "status": 0,

            "msg": "success",

            "data": null

        },

        "10001": {

            "status": 10001,

            "msg": "用户名不能为空",

            "data": null

        },

        "10002": {

            "status": 10002,

            "msg": "密码不能为空",

            "data": null

        },

        "10003": {

            "status": 10003,

            "msg": "密码长度不能小于8位",

            "data": null

        }

    }],

    "/product/list":[

        {

        "10000":{

            "status": 10000,

            "msg": "SUCCESS",

            "data": {

                "total": 2,

                "list": [

                    {

                        "id": 17,

                        "name": "红颜奶油草莓 约重500g/20-24颗 新鲜水果",

                        "image": "http://127.0.0.1:8083/images/caomei2.jpg",

                        "detail": "商品毛重:0.58kg商品产地:丹东/南通/武汉类别:红颜草莓包装:简装国产/进口:国产",

                        "categoryId": 11,

                        "price": 99,

                        "stock": 82,

                        "status": 1,

                        "createTime": "2019-12-18T08:11:13.000+0000",

                        "updateTime": "2020-03-07T10:40:55.000+0000"

                    },

                    {

                        "id": 2,

                        "name": "澳洲进口大黑车厘子大樱桃包甜黑樱桃大果多汁 500g 特大果",

                        "image": "http://127.0.0.1:8083/images/chelizi2.jpg",

                        "detail": "商品毛重:1.0kg货号:608323093445原产地:智利类别:美早热卖时间:1月,11月,12月国产/进口:进口售卖方式:单品",

                        "categoryId": 14,

                        "price": 50,

                        "stock": 1001,

                        "status": 1,

                        "createTime": "2019-12-18T08:08:15.000+0000",

                        "updateTime": "2020-03-07T10:44:26.000+0000"

                    }

                ],

                "pageNum": 1,

                "pageSize": 10,

                "size": 2,

                "startRow": 1,

                "endRow": 2,

                "pages": 1,

                "prePage": 0,

                "nextPage": 0,

                "isFirstPage": true,

                "isLastPage": true,

                "hasPreviousPage": false,

                "hasNextPage": false,

                "navigatePages": 8,

                "navigatepageNums": [

                    1

                ],

                "navigateFirstPage": 1,

                "navigateLastPage": 1

            }

        }

    }

]


        

    

}





handle_result.py文件如下:

#取得code码对应的预期json

    def handleExpectJson(self,key,code):

        data = jsonsOperator.getValue(key,"/chapter7/config/result.json")

        print(data)

        if data:

            for i in data:

                msg = i.get(code)

                if msg:

                    return msg

        return None





run_main.py:

if(expectWay == "json"):

                     #status是int型,一定要转化为str类型

                     expect = handleResult.handleExpectJson(url,str(status))

                     if handleResult.handleJson(res,expect):

                         print(caseId,"按照json格式验证通过")

                         print("res is----------->",res)

                         print("expect is----------->",expect)

                     else:

                         print(caseId,"按照json格式验证失败")

                         print("res is----------->",res)

                         print("expect is----------->",expect)



写回答

1回答

Mushishi

2021-07-04

如果你们code和结果是一一匹配的那么是可以的。

0
1
cloudonthesun
非常感谢!
2021-07-04
共1条回复

Python接口自动化测试框架实战 从设计到开发

接口自动化测试框架的设计与开发,并教你如何开发桩服务

1720 学习 · 831 问题

查看课程