python+requests+excel 接口自动化测试框架实际应用

前言:该篇主要用三个真实的接口进行测试,暂未用到数据依赖

一、 修改测试用例文件如下:

二、 修改请求数据user.json文件如下:

三、修改operation_header.py文件如下:

import requests
import json
from utils.operation_json import OperationJson


class OperationHeader(object):
    def __init__(self, response):
        self.response = json.loads(response)

    # 获取登录返回的响应数据中url的内容,根据实际接口情况来编写该方法
    def get_response_url(self):
        url = self.response['token']
        return url

    def write_cookie(self):
        op_json = OperationJson()
        token = self.response
        op_json.write_data(token)

四、修改run_test.py文件如下:

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
from base.run_method import RunMethod
from dataconfig.get_data import GetData
from utils.header import OperationHeader
from utils.operation_json import OperationJson
from utils.common_util import CommonUtil
from utils.send_mail import SendEmail


class RunTest(object):
    def __init__(self):
        self.data = GetData()
        self.run_method = RunMethod()
        self.com_util = CommonUtil()
        self.send_mail = SendEmail()

    # 执行程序
    def go_on_run(self):
        pass_count = []
        fail_count = []
        # 下标从0开始
        rows_count = self.data.get_case_lines()
        # 从下标1开始循环取数据,忽略第一行表头数据
        for i in range(1, rows_count):
            is_run = self.data.get_is_run(i)
            if is_run:
                method = self.data.get_request_method(i)
                url = self.data.get_url(i)
                # data = self.data.get_request_data(i)
                data = self.data.get_data_from_json(i)
                headers = self.data.get_is_header(i)
                # expect = self.data.get_expect_data_from_mysql(i)
                expect = self.data.get_expect_data(i)
                # depend_case = self.data.get_is_depend(i)
                if headers == "write":
                    res = self.run_method.run_main(method, url, data)
                    op_header = OperationHeader(res)
                    op_header.write_cookie()

                elif headers == "yes":
                    op_json = OperationJson('../data/cookie.json')
                    token = op_json.get_data("token")
                    headers = {
                        "token": token
                    }
                    # print(method)
                    # print(url)
                    print(headers)
                    print(data)
                    res = self.run_method.run_main(method, url, data, headers=headers)
                    print(res)
                else:
                    res = self.run_method.run_main(method, url, data)

                if self.com_util.is_contain(expect, res):
                    self.data.write_result(i, 'pass')
                    pass_count.append(i)
                else:
                    self.data.write_result(i, res)
                    fail_count.append(i)
        print("成功的用例数:", len(pass_count))
        print("失败的用例数:", len(fail_count))
        # self.send_mail.send_main(pass_count, fail_count)


if __name__ == '__main__':
    run = RunTest()
    run.go_on_run()

测试结果如下:

cookie.json文件内容:

测试用例结果:

发布了26 篇原创文章 · 获赞 24 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/changyixue/article/details/100698309