美文网首页
Python自动化接口测试并导出测试结果

Python自动化接口测试并导出测试结果

作者: SingleYan | 来源:发表于2021-04-22 16:15 被阅读0次

Python自动化接口测试并导出测试结果

首先导入要用到的库

表格数据格式如下

import xlrd (用来读取表格数据)

import requests(用来进行网络请求)

import json 

from xlutils.copy import copy

from datetime import datetime

import time

xlrd requests 需要在控制台进行pip install 安装 并导入

代码如下

timeIndex = 1 (请求次数)

file_path = input('请输入文件路径:') (输入表格路径: xxx/xxx/xxx.xls)

pathIndex = file_path.index('.') (获取.前面的路径)

save_path = file_path[0:pathIndex] + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + file_path[pathIndex: len(file_path)](输出表格,输出位置为输入表格同文件夹,名字为原表格名称+ 运行时间)

file = xlrd.open_workbook(file_path)(打开表格)

print(file)

timeStick = input('请输入间隔时长:')(循环间隔市场 单位 S)

write_data = copy(file)

table = file.sheets()[0](委屈表格中的sheets)

print("ssss", table.nrows)

print(file)

sheet = write_data.get_sheet(0)(获取第一个sheet)

//运用for循环去读取表格每行的数据

def Indsfesx():

    print(file)

    for index in range(1,table.nrows):

    # print(sheet.row_values(index))

      reques = table.row_values(index) //读取每行的数据 类型为dictionary

      print(reques)

      print(reques[1])

      requestType = reques[1] //取出请求类型

      requsertCode = 200

      parsms = json.loads(reques[3]) //取出参数

      url = reques[2] //取出接口地址

      if requestType == 'get':

          print('get请求')

          payload = {}

          headers = {}

          respond = requests.request("GET", url, params=parsms, headers=headers)

          print(respond.json())

          print(respond.status_code)

          # requsertCode = respond.status_code

          sheet.write(index, 6, respond.status_code) //将结果写入表格

          if respond.status_code == 200 :

              requestJson = respond.json()

              print(type(requestJson))

              sheet.write(index, 9, respond.text)

              # requestDic = json.loads(requestJson)

              sheet.write(index, 7, requestJson['code'])

              if requestJson['code'] == 0:

                  sheet.write(index, 8, 'true')//将结果写入表格

              else:

                  sheet.write(index, 8, 'fasle')//将结果写入表格

          # sheet.write(i, 6, requsertCode)

          #        sheet.write(index, 9, respond.text)

      else:

          print('post请求')

          response = requests.post(url, data=parsms, headers=headers)

          print(response.json())

          sheet.write(index, 9, response.text)

          # print(respond.status_code)

          sheet.write(index, 6, response.status_code)

          if response.status_code == 200:

            requestJson = response.json()

            print(type(requestJson))

            sheet.write(index, 7, requestJson['code'])

            if requestJson['code'] == 0:

              sheet.write(index, 8, 'true')

            else:

              sheet.write(index, 8, 'fasle')

          else:

              sheet.write(index, 8, 'fasle')

def timer(n):

    bools = True

    while bools:

        print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

        time.sleep(n)  // 计时器间隔设置 

        Indsfesx()

        global timeIndex

        timeIndex = timeIndex + 1

        if timeIndex == 5:

            bools = False

Indsfesx()

write_data.save(save_path) // 将table 写入新的文件

timer(int(timeStick))

运行效果 :

相关文章

  • Python自动化接口测试并导出测试结果

    Python自动化接口测试并导出测试结果 首先导入要用到的库 表格数据格式如下 import xlrd (用来读取...

  • 《老兵爱学习》【专题目录】

    “老兵爱学习”之颠覆你的Python接口自动化测试,约吗? “老兵爱学习”之《颠覆你的Python接口自动化测试》...

  • 需要掌握的知识

    编程,python和java 测试能力:测试方法,测试思路,测试流程等 自动化能力:接口测试,性能测试,压力测试,...

  • 第七次分享06.28 接口测试(Python)

    听了五娃的接口测试&通过Python实现接口自动化测试,知识梳理整理如下: 1.接口测试,根据自己所在公司的实际情...

  • yaml文件保存的接口参数处理

    接口自动化测试-二次处理yaml文件中保存的接口地址及数据 利用python做接口自动化测试需要用到request...

  • 可爱的python测试开发库

    python、软件测试等海量书籍 接口自动化性能测试线上培训大纲 Table of Contents Table ...

  • 接口测试一:接口测试流程

    接口测试一:接口测试流程 学习目标:了解接口测试自动化测试的流程接口自动化测试的流程1、需求分析:了解有有哪些接口...

  • python接口自动化-实战(第一阶段)

    全套视频便宜甩卖,web 接口 app自动化测试,python全栈自动化测试 目标 加深对自动化的理解功能自动化:...

  • 产品经理

    大三暑假,作为一名追求高薪,手握python,selenium自动化测试,爬虫,性能测试,接口测试,Appium移...

  • 接口

    最近开始学习Python自动化测试,第一个就是接口测试,每天测试中都接触接口,但是接口具体有哪些呢?我从网络上搜罗...

网友评论

      本文标题:Python自动化接口测试并导出测试结果

      本文链接:https://www.haomeiwen.com/subject/hkhfrltx.html