• 接口自动化测试 python+request+excel(七)


    前面学习了读取Excel,这次将返回数据写入Excel中,然后进行判断,该条测试用例是否通过

    封装写入Excel,在handle_excel.py加入,之前预留了write_value,现在就可以进行封装,在前面加上

    from xlutils.copy import copy

    from xlutils.copy import copy
        # 向某个单元格写入数据
        def write_value(self, row, col, value):
            data = xlrd.open_workbook(self.file)    # 打开文件
            data_copy = copy(self.file)             # 复制源文件
            sheet = data_copy.get_sheet(0)          # 取得复制文件的sheet对象
            sheet.write(row, col, value)            # 在某一单元格写入value
            data_copy.save(self.file)               # 保存文件

    如果打不开Excel文件,请看解决办法:https://www.cnblogs.com/hemingwei/p/11585799.html

    在主函数run_handle_excel.py加入写入Excel的方法

    #!/usr/bin/env python3
    # -*-coding:utf-8-*-
    # __author__: hunter
    
    from conn.run_demo import RunMain
    from interface.tool.handle_excel import *
    
    
    import json
    
    
    class RunTestCase:
        def __init__(self):
            self.Runmain = RunMain()        # 实例化调用get/post请求基类
            self.data = HandleExcel()       # 实例化操作Excel文件类
    
        def go_run(self):
            rows_count = self.data.get_rows()                   # 获取Excel行数
            for i in range(1, rows_count):                      # 利用行数进行迭代处理每个接口
                url = self.data.get_value(i, get_url())         # 循环获取URL的值
                method = self.data.get_value(i, get_mothod())   # 循环获取method的值
                print(self.data.get_value(i, get_params()))
                data = json.loads(self.data.get_value(i, get_params()))     # 循环获取请求参数
                expect = self.data.get_value(i, get_expectvalue())          # 循环获取期望输出
                is_run = self.data.get_value(i, get_priority())  # 获取是否运行,即判断Excel中priority是不是为“high"
                if is_run == 'high':
                    res = self.Runmain.run_main(url, method, data)      # 调用主函数,res就是返回的参数
                    self.data.write_value(i, get_actualvalue(), res)    # 将实际结果写入Excel中
                    if expect in res:           # res返回的内容是否包含expect,是否与期望一致
                        print('测试通过')
                        self.data.write_value(i, get_resultvalue(), 'pass') # 调用写入数据方法,将结果写进Excel
                    else:
                        print("测试失败")
                        self.data.write_value(i, get_resultvalue(), 'fail')
    
    
    if __name__ == '__main__':
        run = RunTestCase()
        run.go_run()

    得到结果:

     

  • 相关阅读:
    解决ubuntu不能安装g++的问题
    解决VMware虚拟机不能上网的问题
    打开vmvare出现The VMware Authorization Service is not running。
    word2-寻找社交新浪微博中的目标用户
    新浪云计算SAE部署代码过程
    Python如何调用新浪api接口的问题
    work1-英语辅导班在线报名系统
    Mysql对自增主键ID进行重新排序
    如何使用LIBSVM,从安装到基本实例使用
    laravel怎么创建一个简单的blog
  • 原文地址:https://www.cnblogs.com/hemingwei/p/11592334.html
Copyright © 2020-2023  润新知