python写接口自动化框架

代码如下:

  1 #!/usr/bin/env python
  2 # -*- coding: utf_8 -*-
  3 # 获取测试用例文件excel
  4
  5 import xlrd
  6 import json
  7
  8
  9 class CreateExcel:
 10     def __init__(self):
 11         pass
 12
 13     @classmethod
 14     def open_excel(cls):
 15         path = "testcase.xls"
 16         workbook = xlrd.open_workbook(path)
 17         table = workbook.sheets()[0]
 18         return table
 19
 20     # 获取sheet
 21
 22     @classmethod
 23     def get_nrows(cls, table):
 24         nrows = table.nrows
 25         return nrows
 26
 27     # 获取行号
 28
 29     @classmethod
 30     def get_id(cls, table, nrows):
 31         testid = []
 32         for i in range(1, nrows):
 33             testid.append(table.cell(i, 0).value)
 34         return testid
 35
 36     @classmethod
 37     def get_name(cls, table, nrows):
 38         testname = []
 39         for i in range(1, nrows):
 40             testname.append(table.cell(i, 1).value)
 41         return testname
 42
 43     # 获取用例name
 44
 45     @classmethod
 46     def get_data(cls, table, nrows):
 47         testdata = []
 48         for i in range(1, nrows):
 49             try:
 50                 data = json.loads(table.cell(i, 2).value)
 51                 testdata.append(data)
 52             except ValueError:
 53                 testdata.append(None)
 54         return testdata
 55
 56     # 获取data接口参数
 57
 58     @classmethod
 59     def get_url(cls, table, nrows):
 60         testurl = []
 61         for i in range(1, nrows):
 62             testurl.append(table.cell(i, 3).value)
 63         return testurl
 64
 65     # 获取接口测试url
 66
 67     @classmethod
 68     def get_method(cls, table, nrows):
 69         testmethod = []
 70         for i in range(1, nrows):
 71             testmethod.append(table.cell(i, 4).value)
 72         return testmethod
 73
 74     # 获取接口测试method
 75
 76     @classmethod
 77     def get_pattern(cls, table, nrows):
 78         testpattern = []
 79         for i in range(1, nrows):
 80             testpattern.append(table.cell(i, 5).value)
 81         return testpattern
 82
 83     # 获取接口期望响应结果
 84 

  1 #!/usr/bin/env python
  2 # -*- coding: utf_8 -*-
  3 # 测试核心组件
  4
  5 import requests
  6 import re
  7 from datetime import datetime
  8 from createexcel import CreateExcel
  9 from xml.dom import minidom
 10 import sys
 11
 12
 13 class CreateTest:
 14     reload(sys)
 15     sys.setdefaultencoding("utf-8")
 16
 17     # 避免字符串写入文件出错
 18
 19     def __init__(self):
 20         pass
 21
 22     @classmethod
 23     def test_api(cls, method, url, data):
 24         global results
 25         try:
 26             if method == "post":
 27                 results = requests.post(url, data)
 28             if method == "get":
 29                 results = requests.get(url, data)
 30             return results
 31         except Exception.__bases__:
 32             print "服务器访问失败"
 33
 34     # 接口函数
 35
 36     @classmethod
 37     def test_on(cls):
 38         print "用例执行开始"
 39
 40     @classmethod
 41     def test_close(cls):
 42         print "用例执行结束"
 43
 44     @classmethod
 45     def test_result(cls, pa):
 46         global report
 47         try:
 48             pattern = re.compile(pa)
 49             match = pattern.search(testresults.text)
 50             if match.group() == pa:
 51                 report = "测试通过"
 52         except AttributeError:
 53             report = "测试失败"
 54         return report
 55
 56     # 正则表达式检测
 57
 58     @classmethod
 59     def test_http(cls, code):
 60         print "请求返回状态码: ", code
 61
 62     @classmethod
 63     def test_time(cls):
 64         nowtime = datetime.today()
 65         time = nowtime.strftime("%Y-%m-%d %H:%M:%S")
 66         return time
 67
 68     # 获取当前时间转化字符串
 69
 70     @classmethod
 71     def test_report(cls):
 72         nowtime = datetime.today()
 73         reportime = nowtime.strftime("%Y%m%d%H%M%S")
 74         reportname = reportime + ".xml"
 75         return reportname
 76
 77     # 获取测试报告文件名称
 78
 79     @classmethod
 80     def test_main(cls):
 81         global testresults
 82         table = CreateExcel.open_excel()
 83         nrows = CreateExcel.get_nrows(table)
 84         xml = minidom.Document()
 85         xml.appendChild(xml.createComment("测试报告"))
 86         caselist = xml.createElement("caselist")
 87         xml.appendChild(caselist)
 88         for i in range(0, nrows - 1):
 89             testid = CreateExcel.get_id(table, nrows)[i]
 90             testname = CreateExcel.get_name(table, nrows)[i]
 91             testdata = CreateExcel.get_data(table, nrows)[i]
 92             testurl = CreateExcel.get_url(table, nrows)[i]
 93             testmethod = CreateExcel.get_method(table, nrows)[i]
 94             testpattern = CreateExcel.get_pattern(table, nrows)[i]
 95
 96             # 执行测试
 97             CreateTest.test_on()
 98             testresults = CreateTest.test_api(testmethod, testurl, testdata)
 99             testcode = str(testresults.status_code)
100             try:
101                 CreateTest.test_http(testresults.status_code)
102             except AttributeError:
103                 pass
104             CreateTest.test_close()
105             # 执行结束
106             # 生成xml文件
107             case = xml.createElement("case")
108             case.setAttribute("id", testid)
109             # 输入用例ID
110
111             name = xml.createElement("name")
112             name.appendChild(xml.createTextNode(testname))
113             # 输入用例名称
114             method = xml.createElement("method")
115             method.appendChild(xml.createTextNode(testmethod))
116             # 输入接口类型
117             code = xml.createElement("code")
118             code.appendChild((xml.createTextNode(testcode)))
119             # 输入用例返回状态码
120             result = xml.createElement("result")
121             result.appendChild(xml.createTextNode(CreateTest.test_result(testpattern)))
122             # 输入用例测试结果
123             time = xml.createElement("time")
124             time.appendChild(xml.createTextNode(CreateTest.test_time()))
125             # 输入用例执行时间
126
127             case.appendChild(name)
128             case.appendChild(method)
129             case.appendChild(code)
130             case.appendChild(result)
131             case.appendChild(time)
132
133             caselist.appendChild(case)
134             # xml文件生成结束
135         filename = file(CreateTest.test_report(), "w+")
136         # 生成以当前时间命名的测试报告文件
137         xml.writexml(filename)
138         filename.close()
139         # 关闭文件
140
141
142 if __name__ == ‘__main__‘:
143     CreateTest.test_main()

下面是测试入口:

 1 #!/usr/bin/env python
 2 # -*- coding: utf_8 -*-
 3 # ****************************************************************
 4 # interface.py
 5 # Author     : ChenLei
 6 # Version    : 2.0
 7 # Date       : 2016-4-15
 8 # ****************************************************************
 9
10 import time
11 from createtest import CreateTest
12
13 start = time.clock()
14 CreateTest.test_main()
15 end = time.clock()
16
17 print "接口自动化脚本运行时间:%.03f seconds" % (end - start)

运行后自动生成 当前时间的xml文件 如下:

分类: python

时间: 2024-08-06 11:57:20

python写接口自动化框架的相关文章

python+requests接口自动化测试框架实例详解教程

转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实现的接口自动化框架详细教程 前段时间由于公司测试方向的转型,由原来的web页面功能测试转变成接口测试,之前大多都是手工进行,利用postman和jmeter进行的接口测试,后来,组内有人讲原先web自动化的测试框架移驾成接口的自动化框架,使用的是java语言,但对于一个学java,却在学python的我来说,觉得python比起java更简单些,所以,我决定自

python3+request接口自动化框架

首次书写博客,记录下写的自动化接口框架,框架比较简单,哈哈哈,算是记录下历程把!~~~ 一.本次框架由python3.6 书写 1.准备代码环境,下载python3.6    下载地址:https://www.python.org/downloads 2.下载pycharm 软件. 二.开始创建python接口自动化框架: 1.这是我创建的框架中的各个文件夹,分别有config  配置文件夹.fengzhuang   将接口用get post  两种传输方式进行封装并自动来区分执行. 2.log

python+requests接口自动化测试框架实例详解

前段时间由于公司测试方向的转型,由原来的web页面功能测试转变成接口测试,之前大多都是手工进行,利用postman和jmeter进行的接口测试,后来,组内有人讲原先web自动化的测试框架移驾成接口的自动化框架,使用的是java语言,但对于一个学java,却在学python的我来说,觉得python比起java更简单些,所以,我决定自己写python的接口自动化测试框架,由于本人也是刚学习python,这套自动化框架目前已经基本完成了,于是进行一些总结,便于以后回顾温习,有许多不完善的地方,也遇到

浅析Python进行接口自动化

python进行接口自动化需要依赖于requests库,首先如果没有下载该库,使用pip进行下载到本地:1.进入D:\软件安装文件夹\PYTHON\Scripts目录下(ps:这个看个人安装python位置),使用cmd进入命令界面,输入pip install requests,完成后即可使用 参考示例: 期间自己调测可以使用fiddler抓包进行调试,后面再研究使用unitest测试框架进行校验等 原文地址:https://blog.51cto.com/12390959/2369129

接口自动化框架好文

接口自动化框架好文 2017-04-13 API 自动化测试框架分享 接口测试的一些感悟 HTTP API自动化测试从手工到平台的演变

基于python的接口自动化实战(一)

最近接触了接口自动化,经过大约一个月的时间,利用工作之余,借助公司的项目,搭建了接口自动化框架(此框架是要实现脚本与数据的完全分离).整个过程中,最重要的就是实现思路,思路有了,实现起来还是不困难的. 第一篇就先记录一下搭建框架的大致思路. 1.首先,整个框架应该包含的内容 简单概括:执行测试用例,生成测试报告 2.怎么实现测试用例的执行 本框架中,使用Excel管理测试用例,所以整个框架的核心就是获取Excel表格中需要的数据(或者说获取执行每一个接口测试需要的数据) 整个的框架如下图所示:

python接口自动化框架

基于 python 的接口测试框架 接口测试 · jphtmt · 于 5 月前发布 · 最后由 jphtmt 于 4 月前回复 · 3553 次阅读 项目背景 公司内部的软件采用B/S架构,管理实验室数据,实现数据的存储和分析统计.大部分是数据的增删改查,由于还在开发阶段,所以UI界面的变化非常快,之前尝试过用python+selenium进行UI自动化测试,后来发现今天刚写好的脚本第二天前端就改了页面,又得重新去定位元素什么的,消耗大量的精力与时间维护自动化脚本.针对此种情况,对接口测试较为

华为测试大牛Python+Django接口自动化怎么写的?

有人喜欢创造世界,他们做了开发者:有的人喜欢开发者,他们做了测试员.什么是软件测试?软件测试就是一场本该在用户面前发生的灾难提前在自己面前发生了,这会让他们生出一种救世主的感觉,拯救了用户,也就拯救者这个软件,避免了他们被卸载的命运. 最近被几个公司实习生整自闭了,没有基础,想学自动化又不知道怎么去学,没有方向没有头绪,说白了其实就是学习过程中没有成就感,所以学不下去.出于各种花里胡哨的原因,今天给大家整一个简单又有成就感的接口自动化学习吧. 不皮了,进入正题.本文中用到的技术点有:Python

python+requests接口自动化完整项目设计源码

前言 有很多小伙伴吵着要完整的项目源码,完整的项目属于公司内部的代码,这个是没法分享的,违法职业道德了,就算别人分享了,也只适用于本公司内部的业务. 所以用例的代码还是得自己去一个个写,我只能分享项目框架,只能帮你们到这了. 一.项目结构 1.新建一个工程(一定要创建工程),工程名称自己定义,如:yoyo_jiekou 2.在工程的跟目录新建一个脚本:run_main.py,用来执行全部用例 3.在工程下创建以下几个pakage包: --case:这个包放test开头的测试用例,也可以放一些封装