FavoriteLoading
0

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

基于我的 python 接口测试框架

这个版本的优化

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

核心代码


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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]))

邮箱配置


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

成果展示


邮件接收附件

下个版本优化

声明:本文转载自 TesterHome 移动测试社区,作者为 TesterHome 移动测试社区,原文网址:https://testerhome.com/topics/5786

最后编辑于:2016/11/16作者: 聚合

聚合类文章源自互联网, 感谢原作者的无私分享。

关注微信公众号 – 聚合软件测试类精华

关注微信公众号 – 聚合软件测试类精华