接口测试 python 接口测试框架测试报告 (二)

测试小书童 · 2016年08月26日 · 最后由 测试小书童 回复于 2016年09月09日 · 2057 次阅读

基于我的 python 接口测试框架

这个版本的优化

  • 测试报告基于 excel
  • 测试通过后用 email 的方式通知

核心代码

import unittest
import time
from controller import config
from model.common import Goals as go
from controller import con_api_xml
from controller import check
import BaseExcelReport as be
import xlsxwriter
import sendMail as sd
from controller.getEmail import read_email
gm = con_api_xml.ret_xml() # 读取xml
hb = con_api_xml.ret_http_base(gm) #读取http参数
data = {"info":[]}
# 测试用例(组)类
class TestInterfaceCase(unittest.TestCase):
    def __init__(self, testName, hope, index):
        super(TestInterfaceCase, self).__init__(testName)
        self.hope = hope
        self.index = index
    def setUp(self):
        self.config_http = config.ConfigHttp(hb.host, hb.port)
    def function(self):
        response = ""
        if self.index == 1:
             if gm[self.index]["method"] == "POST":
                response = self.config_http.post(go.URL, go.PARAMS)
                go.REALLY_RESULT = eval(response)
                hope = eval(self.hope)
                # temp = testJson.compareJson(hope, go.REALLY_RESULT, gm[self.index]["isList"])
                temp = check.compare(hope, go.REALLY_RESULT)
                if temp:
                    go.LOGIN_KY = gm[1]["login"]
                    go.LOGIN_VALUE = go.REALLY_RESULT["content"][0][go.LOGIN_KY]
                    go.RESULT = 'Pass'
                    go.SUCCESS_SUM += 1
                else:
                    go.RESULT = 'Fail'
                    go.ERROR_NUM += 1
        else:
            if gm[self.index]["login"] != "0":
                    go.PARAMS[go.LOGIN_KEY] = go.LOGIN_VALUE
            if gm[self.index]["method"] == "POST":
                response = self.config_http.post(go.URL, go.PARAMS)
            if gm[self.index]["method"] == "GET":
                response = self.config_http.get(go.URL, go.PARAMS)
            go.REALLY_RESULT = eval(str(response))
            hope = eval(self.hope)
            # temp = testJson.compareJson(hope, go.REALLY_RESULT, gm[self.index]["isList"])
            temp = check.compare(hope, go.REALLY_RESULT,  gm[self.index]["isList"])
            if temp:
                go.RESULT = 'Pass'
                go.SUCCESS_SUM += 1
            # except AssertionError:
            else:
                go.RESULT = 'Fail'
                go.ERROR_NUM += 1
        go.CASE_TOTAL += 1

# 获取测试套件
def get_test_suite(index):
    test_suite = unittest.TestSuite()
    hope = gm[index]["hope"] # 预期值
    test_suite.addTest(TestInterfaceCase("function", hope,index))
    return test_suite

# 运行测试用例函数
def run_case(runner):
    case_list = hb.No
    case_list = eval(case_list)  # 把字符串类型的list转换为list
    temp_case = ""
    if len(case_list) == False: #判断是否执行指定的用例ID
        temp_case = gm
        for index in range(1, len(temp_case)):
            info = {}
            go.URL = gm[index]['url']
            go.PARAMS = gm[index]["params"]
            test_suite = get_test_suite(index)
            runner.run(test_suite)
            # 记录运行结果
            info["t_id"] = gm[index]["id"]
            info["t_name"] = gm[index]["name"]
            info["t_url"] = gm[0]["host"] + gm[index]["url"]
            info["t_param"] = gm[index]["params"]
            info["t_actual"] = go.REALLY_RESULT
            info["t_hope"] = gm[index]["hope"]
            info["t_result"] = go.RESULT
            info["t_method"] = gm[index]["method"]
            data["info"].append(info)
    else:
        for i in case_list:
            for j in range(1, len(gm)):
                if str(i) == gm[j]['id']:
                    info = {}
                    go.URL = gm[j]['url']
                    go.PARAMS = gm[j]["params"]
                    test_suite = get_test_suite(j)
                    runner.run(test_suite)
                    info["t_id"] = gm[j]["id"]
                    info["t_name"] = gm[j]["name"]
                    info["t_url"] = gm[0]["host"] + gm[j]["url"]
                    info["t_param"] = gm[j]["params"]
                    info["t_actual"] = go.REALLY_RESULT
                    info["t_hope"] = gm[j]["hope"]
                    info["t_result"] = go.RESULT
                    info["t_method"] = gm[j]["method"]
                    data["info"].append(info)
# 运行测试套件
if __name__ == '__main__':
    start_time = time.time()
    runner = unittest.TextTestRunner()
    run_case(runner)
    end_time = time.time()
    sum_time = "%.2f" % (end_time - start_time)
    data["test_date"] = str(sum_time) + "毫秒"
    data["test_sum"] = go.CASE_TOTAL
    data["test_failed"] = go.ERROR_NUM
    data["test_version"] = "v2.0.8"
    data["test_pl"] = "python 3"
    data["test_net"] = "本地连接"
    data["test_name"] = gm[0]["title"]
    data["test_success"] = go.SUCCESS_SUM

    workbook = xlsxwriter.Workbook('report.xlsx')
    worksheet = workbook.add_worksheet("测试总况")
    worksheet2 = workbook.add_worksheet("测试详情")
    # 测试报告
    bc = be.xlsxwriterBase(wd=workbook, data=data)
    bc.init(worksheet)
    bc.test_detail(worksheet2)
    bc.close()

    # 发送email
    get_email = read_email("D:\\app\\auto_http34_test\\email.ini")
    sd.send_mail(f="report.xlsx", to_addr=get_email[0], mail_host=get_email[1], mail_user=get_email[2], mail_pass=get_email[3], port=int(get_email[4]))

邮箱配置

[DEFAULT]
to_addr = ["111@qq.com", "2222@126.com"] 发送给谁
mail_host = smtp.qq.com
mail_user = 1111@qq.com
mail_pass = XXXX
port = 465

成果展示


邮件接收附件

下个版本优化

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 16 条回复 时间 点赞

占个楼 问个比较 low 的问题 接口测试的话 都是要开发提供接口吗 比如我现在想自己找 demo 想自己先入门的话 要怎么获取接口 来进行接口测试呢

分享的精神还是值得肯定的。
为什么没有选择 html 发报告呢?

#2 楼 @lucasluo html 限制太多,excel 可以统计更多东西,vb,截图,日志分析,统计分析等等,好几家云测平台也是用的 excel

#1 楼 @y693055797
一开始可以使用工具
可以从简单的发送 http 请求,读取 http 返回数据开始

#4 楼 @lose 那就是一般接口测试还是要开发提供接口吧

#5 楼 @y693055797 自己抓包看也可以

#6 楼 @nickli 有推荐的帖子吗 我搜了很久很少有详细讲自己抓包看的 都是那种单独的实例

整套框架都开源?

#8 楼 @mads 这套东东你自己鼓捣就可以,李哥那边出了套蛮不错的框架,开发在 github 上 push 一把 自动扫描源码后并生成 case 然后。。。你懂的

#9 楼 @kevin_sqa 路径能发个不?

—— 来自 TesterHome 官方 安卓客户端

#9 楼 @kevin_sqa 地址发来看看

#10 楼 @hu_qingen 他没开源 沙龙上他可以讲讲这个

#11 楼 @mads G20 之后 咱们碰碰 你可以当面向他请益啊

虽然你在 shouke 基础上进行了很多优化,但看的出来框架代码部分也是 copy 的,就应该要注明,是对作者起码的尊重,我要是不说,大家还以为是你自己写的框架。为何第一篇文章关闭评论呢?不明真相的吃瓜观众还在说我喷子,额。。。我也是醉了。

#14 楼 @loading 你说的对,,之所以关闭第一篇,是重新开了个优化帖子,哈哈~后面如果出了第三个优化贴(估计很快就要出了),这个帖子也会关了😉

匿名 #16 · 2016年09月06日

接口测试小白,请问下,断言这块,是手动输入正确结果,与返回结果进行对比么?

测试小书童 关闭了讨论 09月09日 15:03
测试小书童 [该话题已被删除] 中提及了此贴 09月09日 15:22
测试小书童 python 接口测试框架测试 (三) 中提及了此贴 01月06日 17:16
测试小书童 关闭了讨论 06月22日 14:23
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册