51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 3581|回复: 0
打印 上一主题 下一主题

[转贴] python 读取 Excel 自动化执行测试用例

[复制链接]
  • TA的每日心情
    擦汗
    昨天 08:46
  • 签到天数: 981 天

    连续签到: 1 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2021-6-28 09:39:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    接口请求代码构建

    1. import requests
    2.   from random import randint
    3.   import xlrd
    4.   import json
    5.   class My_request(object):
    6.       # 对返回值进行处理
    7.       def __str__(self):
    8.           # return "%r"%self.connect()
    9.           return str("%r"%self.connect())
    10.       # 获取表格数据
    11.       def connect(self):
    12.           excelDir = '../data/动画绘本馆测试用例.xls'
    13.           # 打开Excel文件 formatting_info=True :保持原样式
    14.           self.workBook = xlrd.open_workbook(excelDir,formatting_info=True)
    15.           # 获取所有表名
    16.           sheets = self.workBook.sheet_names()
    17.           # 需要执行的sheet
    18.           workSheet = self.workBook.sheet_by_name('测试用例2')
    19.           # 获取一行
    20.           # cellData = workSheet.row_values(1)
    21.           # 获取单元格 获取的不是字符串 而是excel 格式参数
    22.           cellData = workSheet.cell(1, 0)
    23.           return cellData.value
    24.       # token函数 调用函数就可获取token值 再用到新增接口
    25.       def get_token(self):
    26.           # 获取token
    27.           token_urls = 'http://47.96.181.17:9090/rest/toController'
    28.           payload = {'userName': 'J201903070064', 'password': '362387359'}
    29.           # 发送请求 json 请求体
    30.           res = requests.post(token_urls, json=payload)
    31.           return res.json()['token']
    32.       #  新增用户接口
    33.       def add_user(self,bodyData):
    34.           """
    35.           :param bodyData: request body
    36.           :return:  res data
    37.           """
    38.           self.addUsr_url = 'http://47.96.181.17:9090/rest/ac01CrmController'
    39.           # 对字符串转换字典 json 串
    40.           self.payload = json.loads(bodyData)
    41.           # 对手机号进行参数化
    42.           self.payload['aac030'] = f'134{randint(11111111,99999999)}'
    43.           self.header_addusr = {'Content-Type': 'application/json', 'X-AUTH-TOKEN': self.get_token()}
    44.           # # 发送请求 url 请求体
    45.           res = requests.post(self.addUsr_url,json=self.payload,headers = self.header_addusr)
    46.           self.rest = res.json()
    47.           # 打印输出结果 是否注册成功
    48.           print(res.json())
    49.           return res.json()
    50.       def write_index(self,datas):
    51.           from xlutils.copy import copy
    52.           # 对表复制 原始的测试用例不会改变
    53.           newWoekBook = copy(self.workBook)
    54.           # 取拷贝的excel的sheet 下标
    55.           newSheet = newWoekBook.get_sheet(1)
    56.           # 写入数据 第一行第一列
    57.           newSheet.write(1, 1, datas)
    58.           # 保存新生成的 excel对象
    59.           newWoekBook.save('../data/res.xls')
    60.       def write_in(self):
    61.           # 如果已经有数据了 就需要对原表进行复制
    62.           if self.rest['message'] == '成功':
    63.               info = 'pass'
    64.               self.write_index(info)
    65.           else:
    66.               info = 'fail'
    67.               self.write_index(info)
    68.   if __name__ == '__main__':
    69.       re = My_request()
    70.       re.add_user(re.connect())
    71.       re.write_in()
    复制代码

    测试结果写入到Excel
      文件不存在 -- 新建excel -- 写 -- xlwt
      文件本身存在--另存为写入excel--xlutils
    1.  def write_index(self,datas):
    2.       from xlutils.copy import copy
    3.       # 对表复制 原始的测试用例不会改变
    4.       newWoekBook = copy(self.workBook)
    5.       # 取拷贝的excel的sheet 下标
    6.       newSheet = newWoekBook.get_sheet(1)
    7.       # 写入数据 第一行第一列
    8.       newSheet.write(1, 1, datas)
    9.       # 保存新生成的 excel对象
    10.       newWoekBook.save('../data/res.xls')
    11.   def write_in(self):
    12.       # 如果已经有数据了 就需要对原表进行复制
    13.       if self.rest['message'] == '成功':
    14.           info = 'pass'
    15.           self.write_index(info)
    16.       else:
    17.           info = 'fail'
    18.           self.write_index(info)
    复制代码




    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-7-9 07:30 , Processed in 0.064001 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表