API接口测试之脚本

发表于:2020-10-10 09:36  作者:爻爻牧牧   来源:CSDN

字体: | 上一篇 | 下一篇 |我要投稿 | 推荐标签: 软件测试技术 接口测试

  API接口测试脚本
  Excel处理
# coding:utf-8

import openpyxl
from openpyxl import load_workbook


def write(filename, row, value, sheetname):
    wb = load_workbook(filename)
    # ws = wb.get_active_sheet()
    ws = wb.get_sheet_by_name(sheetname)
    ws[row] = value
    wb.save(filename)


class ExcelReader:
    def __init__(self, excel_path, sheet_name='sheet1'):
        self.wb = openpyxl.load_workbook(excel_path)
        self.ws = self.wb.get_sheet_by_name(sheet_name)
        # self.ws_current = self.ws.active

        self.rowNum = self.ws.max_row
        self.colNum = self.ws.max_column

    def list_data(self):
        if self.rowNum <= 1:
            print("总行数小于1行")
        else:
            r = []
            # j = 1
            for row in range(2, self.rowNum+1):
                c = self.ws['B'+str(row)].value
                r.append(c)
            return r


if __name__ == "__main__":
    filepath = "D:\\debug_api.xlsx"
    sheetName = "Sheet1"
    data = ExcelReader(filepath, sheetName)
    data_deal = data.list_data()
    print(data_deal)
    print(len(data_deal))
    for i in range(2, len(data_deal)+2):
        write("D:\\debug_api.xlsx", 'F'+str(i), 'WRIGHT', '结果-数据')
  API接口
def get_response():
    """Make an api call, and return the response."""
    url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
    headers = {'Accept': 'application/vnd.github.v3+json'}
    r = requests.get(url, headers=headers)
    return r


def get_repo_dicts(r):
    """Return a set of dicts representing the most popular repositories."""
    response_dict = r.json()
    repo_dicts = response_dict['items']
    return repo_dicts


def get_project_data(repo_dicts):
    """Return data needed for each project in visualization."""
    repo_links, stars, labels = [], [], []
    for repo_dict in repo_dicts:
        repo_name = repo_dict['name']
        repo_url = repo_dict['html_url']
        repo_link = f"<a href='{repo_url}'>{repo_name}</a>"
        repo_links.append(repo_link)

        stars.append(repo_dict['stargazers_count'])

        owner = repo_dict['owner']['login']
        description = repo_dict['description']
        label = f"{owner}<br />{description}"
        labels.append(label)

    return repo_links, stars, labels


if __name__ == '__main__':
    r = get_response()
    repo_dicts = get_repo_dicts(r)
    repo_links, stars, labels = get_project_data(repo_dicts)
    value3 = [['URL', '星星', '标签'], repo_links, stars, labels]
    wt = WriteExcel()
    wt.write_excel('d:\\abc.xlsx', '人员信息表', value3)
    print('数据插入完毕')

  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理

评 论

论坛新帖

顶部 底部


建议使用IE 6.0以上浏览器,800×600以上分辨率,法律顾问:上海信义律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2021, 沪ICP备05003035号
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪公网安备 31010102002173号

51Testing官方微信

51Testing官方微博

扫一扫 测试知识全知道