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是测试用例摘要。

 

三.公共函数存在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文件了

图表总览:

 

excel测试报告情况:

 

​现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:485187702【暗号:csdn11】

最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 希望能帮助到你!【100%无套路免费领取】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值