脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python接口自动化框架实战

python接口自动化框架实战

2021-08-17 00:37zx Python

这篇文章主要介绍了python接口自动化框架实战,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

     python接口测试的原理,就不解释了,百度一大堆。

    先看目录,可能这个框架比较简单,但是麻雀虽小五脏俱全。

python接口自动化框架实战

各个文件夹下的文件如下:

python接口自动化框架实战

一.理清思路

    我这个自动化框架要实现什么

    1.从excel里面提取测试用例

    2.测试报告的输出,并且测试报告得包括执行的测试用例的数量、成功的数量、失败的数量以及哪条成功了,失败的是哪一个,失败的原因是什么;测试结果的总体情况通过图表来表示。

    3.测试报告用什么形式输出,excel,还是html,还是其他的,这里我选择了excel

    4.配置文件需要配置什么东西

    5.哪些东西可以放入公共函数直接调用。

好的这些思路理清楚之后就可以动手了。

二.首先是配置文件和excel测试用例的设计

    数据与代码分离,也就是数据性的需要作为配置文件可以随时修改。如:接口url,网站登录权限验证信息,数据库信息。全部存入config文件夹下

下面是具体的配置文件信息:

API_url.txt

?
1
2
3
4
5
inserthouse=http://IP:port/scp-mdmapp/house/insertHouse
deletehouse=http://IP:port/scp-mdmapp/house/deleteHouse
batchdeletehouse=http://IP:port/scp-mdmapp/house/batchdeleteHouse
gethouse=http://IP:port/scp-mdmapp/house/getHouse
updatehouse=http://IP:port/scp-mdmapp/house/updateHouse

Authorization.txt

?
1
joxNTIxMTg3MTA3fQ.JyeCMMsM0tOr7exORUNpkZ-FtprjpNBhMtFjUAdnYDnhRfaR6qi3fqVkybyb245zONiTxLOw8jBR60oNUVEbKx9_cut6uDIZMUFYOx6hyyBkY9IXJlutYdo4sSMAKF_MjKsZY7bZNXLHzN0juiezn6iN0hbnbhS-Kv2LYLLZLTs

我的测试用例的设计如下:

notes是测试用例摘要。

python接口自动化框架实战

三.公共函数存在common文件夹下

get_authorization.py

?
1
2
3
4
5
6
#从配置文件获取访问权限信息
def get_Authorization():
    fp = open('D:\person\learn\py\HDapi\config\Authorization.txt')
    info = fp.read()
    fp.close()
    return info

public.py

?
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
import os,xlrd,xlwt,time
 
#通过配置文件里的接口名称来获取接口url的函数
def get_url(api_name):
    fp = open('D:\person\learn\py\HDapi\config\API_url.txt')
#按行读取接口url配置文件
    api_infos = fp.readlines()
    fp.close()
#通过for循环来遍历配置文件里的每一个url,并且返回传入的接口名称相应的url
    for api in api_infos:
#去除因为读取产生的换行空格等
        api_f = api.strip(' \r\n\t')
        api_c = api_f.split('=')
        if api_name == api_c[0]:
            return api_c[1]
 
#通过传入用例名称的文件和excel页面来读取测试用例
def get_case(filename,sheetnum):
    case_dir='D:\\person\\learn\\py\\HDapi\\testcase_excel' + '\\' + filename + '.xlsx'
 
    datas = xlrd.open_workbook(case_dir)
    table = datas.sheets()[sheetnum]
    nor = table.nrows
    nol = table.ncols
    return nor,table
 
#通过xlwt库来设计测试报告并写入excel里面
def write_report():
    workbook = xlwt.Workbook(encoding='utf-8')
#在excel测试报告表格中创建名叫housemanage的页面
    worksheet = workbook.add_sheet('housemanage')
#设置字体格式为居中对齐
    alignment = xlwt.Alignment()
    alignment.horz = alignment.HORZ_CENTER
    alignment.vert = alignment.VERT_CENTER
    style = xlwt.XFStyle()
    style.alignment = alignment
    
#具体的合并哪些单元格并且写入相应的信息
    worksheet.write_merge(0,0,0,7,'测试报告(housemanage)',style)
    worksheet.write_merge(1,10,0,0,'house_manage',style)
    worksheet.write_merge(1,2,1,1,'insethouse',style)
    worksheet.write_merge(3,4,1,1,'updatehouse',style)
    worksheet.write_merge(5,6,1,1,'deletehouse',style)
    worksheet.write_merge(7,8,1,1,'gethouse',style)
    worksheet.write_merge(9,10,1,1,'updatehouse',style)
    worksheet.write_merge(1,2,11,11,'total_result',style)
    worksheet.write(1,2,'notes')
    worksheet.write(2,2,'detail')
    worksheet.write(3,2,'notes')
    worksheet.write(4,2,'detail')
    worksheet.write(5,2,'notes')
    worksheet.write(6,2,'detail')
    worksheet.write(7,2,'notes')
    worksheet.write(8,2,'detail')
    worksheet.write(9,2,'notes')
    worksheet.write(10,2,'detail')
    worksheet.write(1,12,'pass')
    worksheet.write(1,13,'faild')
#最后返回worksheet,workbook两个参数,因为在测试测试用例和运行文件中需要用到的两个参数
    return worksheet,workbook

四.测试用例的编写

test_inserthouse.py

?
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
import requests,unittest,os,time,json
from common import public,get_authorization
 
 
#房屋添加用例,通过传入public里wirte_sheet函数返回的参数wooksheet,将用例的执行结果写入到测试报告中
def test_inserthouses(worksheet,workbook):
    url = public.get_url('inserthouse')
    nor,table = public.get_case('house',0)
    Authorization = get_authorization.get_Authorization()
    a = 2
    xu = 0
    yu = 0
#用for循环来实现遍历一个excel页面的所有测试用例
    for i in range(1,nor):
#获取excel表格里面需要给接口传入的参数
        houseNum = table.cell_value(i,0)
        orgUuid = table.cell_value(i,1)
        floor = table.cell_value(i,2)
        houseUseFor = table.cell_value(i,3)
        residentNum = table.cell_value(i,4)
        emergencyPhone = table.cell_value(i,5)
        expect_code = table.cell_value(i,6)
        expect_message = table.cell_value(i,7)
        notes = table.cell_value(i,8)
        payment = table.cell_value(i,11)
#接口body需要传入的参数
        data = {
        'houseNum':houseNum,
        'houseUseFor':houseUseFor,
        'orgUuid':orgUuid,
        'residentNum':residentNum,
        'floor':floor,
        'emergencyPhone':emergencyPhone,
        'payment':payment
        }
 
#请求头,网站加了登陆验证之后需要在请求头传入Authorization参数
        headers={
        'Accept':'application/json',
        'Content-Type':'application/json',
        'Authorization':Authorization
        }
        a+=1
        worksheet.write(1,a,notes)
 
        data = json.dumps(data)
 
        r = requests.post(url,data=data,headers=headers)
#将字符串格式转换为字典
        b = eval(r.text)
        m = b.get('code')
        n = b.get('message')
        k = b.get('data')
#判断接口测试通过与否
        if m==expect_code and n==expect_message:
            worksheet.write(2,a,'pass')
            xu += 1
        else:
            worksheet.write(2,a,'faild:%s'%k)
            yu += 1
#测试用例执行完后,返回用例成功与失败的数量
    return xu,yu

test_updatehouse.py

?
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
import requests,unittest,os,time,json
from common import public,get_authorization
 
#房屋编辑测试用例
def test_updatehouses(worksheet,workbook):
    nor,table = public.get_case('house',4)
    Authorization = get_authorization.get_Authorization()
    url = public.get_url('updatehouse')
    a = 2
    x = 0
    y = 0
    for i in range(1,nor):
        houseNum = table.cell_value(i,0)
        orgUuid = table.cell_value(i,1)
        uuid = table.cell_value(i,2)
        houseUseFor = table.cell_value(i,3)
        residentNum = table.cell_value(i,4)
        emergencyPhone = table.cell_value(i,5)
        expect_code = table.cell_value(i,6)
        expect_message = table.cell_value(i,7)
        notes = table.cell_value(i,8)
        floor = table.cell_value(i,9)
        payment = table.cell_value(i,11)
 
        data = {
        'houseNum':houseNum,
        'houseUseFor':houseUseFor,
        'orgUuid':orgUuid,
        'floor':floor,
        'residentNum':residentNum,
        'uuid':uuid,
        'emergencyPhone':emergencyPhone,
        'payment':payment
        }
 
        headers={
        'Accept':'application/json',
        'Content-Type':'application/json',
        'Authorization':Authorization
        }
 
        a+=1
        worksheet.write(3,a,notes)
 
        data = json.dumps(data)
 
        r = requests.post(url,data=data,headers=headers)
        b = eval(r.text)
        m = b.get('code')
        n = b.get('message')
        k = b.get('data')
        if m==expect_code and n==expect_message:
            worksheet.write(4,a,'pass')
            x += 1
        else:
            worksheet.write(4,a,'faild:%s'%k)
            y += 1
    return x,y

五.通过对公共函数、测试用例的设计联合的思考应该在执行文件里面做什么,实现什么。

本来我想将执行文件单独放置于HDapi-auto-test的根文件下的,可是将测试通过与不通过的数量写入到测试报告里面,就必须要调用公共函数的方法,由于放置在根文件夹下与公共函数隔了一个文件夹无法调用( 本鸟不会调用),所以不得不将执行文件放置于测试用例文件夹下了,好在文件名还是比较好区分也比较好寻找,另外我还想加上自动发送邮件的功能,这里不写了,其实发送邮件很简单随便找几个例子就OK了,ps:代码比较low,都没有封装,直接暴力简单执行。代码如下:

?
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
from common import public
import test_inserthouse,test_updatehouse
import time
from pychartdir import *
#从公共函数调用excel的写入方法
worksheet,workbook = public.write_report()
 
#测试用例的执行,并且返回x:成功的数量,y:失败的数量
xu,yu = test_inserthouse.test_inserthouses(worksheet,workbook)
x,y = test_updatehouse.test_updatehouses(worksheet,workbook)
#得到成功与失败的总数量
xr = x+xu
yr = y+yu
#将成功与失败的数量写入的excel的固定表格中
worksheet.write(2,12,xr)
worksheet.write(2,13,yr)
#获取当前的时间并以制定的格式返回
now = time.strftime('%Y-%m-%d %H_%M_%S')
#测试报告输出的地址
report_dir = 'D:\\person\\learn\\py\\HDapi\\report\\'
#拼接出测试报告名
filename =report_dir + now + 'apiresult.xlsx'
workbook.save(filename)
 
#通过pychart库实现图形处理,生成测试报告总览图----具体的参数设计可以参考pychart库的文档
data = [yr, xr]
labels = ["faild", "pass"]
c = PieChart(280, 240)
c.setPieSize(140, 130, 80)
c.addTitle("api_result")
c.set3D()
c.setData(data, labels)
c.setExplode(0)
c.makeChart(report_dir+now+"apiresult.png")

六.奉上测试报告输出

本来想将生成的图片放进excel测试报告里面的,奈何能力有限,没办法将图片放进去,智能单独存为一个png文件了

图表总览:

python接口自动化框架实战

excel测试报告情况:

python接口自动化框架实战

到此这篇关于python接口自动化框架实战 的文章就介绍到这了,更多相关python接口自动化内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/liuuil4421640/article/details/79512491

延伸 · 阅读

精彩推荐