.json用例与接口实际返回结果进行对比详细代码

#coding:utf-8
import unittest
from common.gettime import GetTime
import requests
import os
import pandas as pd
import operator

class CompletePhenology(unittest.TestCase):
    '''查询完整生长季物候期'''
    @classmethod
    def setUpClass(cls):
        pass

    @classmethod
    def tearDownClass(cls):
        pass

    def interface(self,payload1):
        g = GetTime()
        token = g.get_time()
        #请求接口地址
        s = requests.session()
        url1 = "111"
        headers1 = {
        "Content-Type": "application/json",
        "Authorization": token
         }
        r = s.post(url=url1,headers=headers1,json=payload1)
        return r

    def test_00(self):
        '''传半角单引号,提示:校验返回结果'''
        payload1= {
            'lon': '127.421549',
            'lat': '46.070227',
            'cropCode': 'spring_cron'
        }
        l = CompletePhenology()
        #登录接口
        j = l.interface(payload1).json()
        d=j["data"]["phenologyList"]
        print(type(d))
        print(d)
        #从文件中读取json数据
        curent_dirc=os.path.dirname(os.path.realpath(__file__))
        case_dir = curent_dirc + "\\test_data"
        with open(case_dir+"\\"+'phenologyaccordcrop.json',encoding='utf-8') as f:
            content = f.read()
        print(type(content))
        list = eval(content)
        # print(type(list))
        # print(list)
        # print(operator.eq(d,list))
        # result = operator.eq(d,list) == True
        # self.assertTrue(result)
        #方法二
        for i in range(len(list)):
            assert list[i].keys() == d[i].keys()
            for key in list[i].keys():
               if list[i][key]!= d[i][key]:
                   expect_result = list[i][key]
                   actual_result = d[i][key]
                   print("预期结果:%s" % expect_result + ",实际结果:%s" % actual_result + ",验证失败!")
                   test_result = list[i][key]!= d[i][key]
                   self.assertFalse(test_result)

 

posted @ 2020-12-03 16:52  Camillezxl  阅读(180)  评论(0编辑  收藏  举报