python接口自动化,对比实际结果与预期结果方法封装

#coding:utf-8
import unittest
import os

class CompareTestCase(unittest.TestCase):
    '''对比测试结果与预期结果是否一致'''

    def compare_data(self,case_name,actual_list,actural_data,case_dir):
        '''数据校验方法'''
        with open(case_dir+"\\"+case_name,encoding='utf-8') as f:
            content = f.read()
        list = eval(content)
        for i in range(len(list)):
            for key in list[i].keys():
               if list[i][key]!= actual_list[i][key]:
                   print("预期结果:%s" % list[i] + ",\n实际结果:%s" % actual_list[i] + ",\n验证结果:失败!")
                   test_result = list[i][key]!= actual_list[i][key]
                   self.assertFalse(test_result)
               else:
                   pass
        print(actural_data)

    def compare_phenology(self,phenology_data_dirc):
        '''获取用例的路径'''
        data_dirc=os.path.dirname(os.path.realpath(__file__))       #读取用例
        phenology_data_dirc = phenology_data_dirc
        case_dir = data_dirc + "\\test_data\\phenology\\" + phenology_data_dirc
        return case_dir

    def compare_for_maturity(self,case_name,actual_result,phenology_data_dirc):
        '''熟性单独调用接口,数据校验接口'''
        #从文件中读取json数据
        data_dirc=os.path.dirname(os.path.realpath(__file__))
        phenology_data_dirc = phenology_data_dirc
        case_dir = data_dirc + "\\test_data\\phenology\\" + phenology_data_dirc
        with open(case_dir+"\\"+case_name,encoding='utf-8') as f:
            content = f.read()
        except_list1 = list(eval(content).values())
        actual_list1 = list(actual_result.values())
        for i in range(len(except_list1)):
            result = except_list1[i]!= actual_list1[i]
            if result:
                print("预期结果:%s" % except_list1 + ",\n实际结果:%s" % actual_list1 + ",\n验证结果:失败!")
                self.assertFalse(result)

 

posted @ 2020-12-08 08:47  Camillezxl  阅读(562)  评论(0编辑  收藏  举报