这个是根据上次框架版本进行的优化

用python获取excel文件中测试用例数据

通过requets测试接口、并使用正则表达式验证响应信息内容

生成xml文件测试报告

版本更新内容:

1. 整理了CreateTest.test_main()流程逻辑

2. 优化了testcase.xls文件格式

3. 添加了生成XML文件测试报告

#!/usr/bin/env python
# -*- coding: utf_8 -*-
# 获取测试用例文件excel import xlrd
import json class CreateExcel:
def __init__(self):
pass @classmethod
def open_excel(cls):
path = "testcase.xls"
workbook = xlrd.open_workbook(path)
table = workbook.sheets()[0]
return table # 获取sheet @classmethod
def get_nrows(cls, table):
nrows = table.nrows
return nrows # 获取行号 @classmethod
def get_id(cls, table, nrows):
testid = []
for i in range(1, nrows):
testid.append(table.cell(i, 0).value)
return testid @classmethod
def get_name(cls, table, nrows):
testname = []
for i in range(1, nrows):
testname.append(table.cell(i, 1).value)
return testname # 获取用例name @classmethod
def get_data(cls, table, nrows):
testdata = []
for i in range(1, nrows):
try:
data = json.loads(table.cell(i, 2).value)
testdata.append(data)
except ValueError:
testdata.append(None)
return testdata # 获取data接口参数 @classmethod
def get_url(cls, table, nrows):
testurl = []
for i in range(1, nrows):
testurl.append(table.cell(i, 3).value)
return testurl # 获取接口测试url @classmethod
def get_method(cls, table, nrows):
testmethod = []
for i in range(1, nrows):
testmethod.append(table.cell(i, 4).value)
return testmethod # 获取接口测试method @classmethod
def get_pattern(cls, table, nrows):
testpattern = []
for i in range(1, nrows):
testpattern.append(table.cell(i, 5).value)
return testpattern # 获取接口期望响应结果
#!/usr/bin/env python
# -*- coding: utf_8 -*-
# 测试核心组件 import requests
import re
from datetime import datetime
from createexcel import CreateExcel
from xml.dom import minidom
import sys class CreateTest:
reload(sys)
sys.setdefaultencoding("utf-8") # 避免字符串写入文件出错 def __init__(self):
pass @classmethod
def test_api(cls, method, url, data):
global results
try:
if method == "post":
results = requests.post(url, data)
if method == "get":
results = requests.get(url, data)
return results
except Exception.__bases__:
print "服务器访问失败" # 接口函数 @classmethod
def test_on(cls):
print "用例执行开始" @classmethod
def test_close(cls):
print "用例执行结束" @classmethod
def test_result(cls, pa):
global report
try:
pattern = re.compile(pa)
match = pattern.search(testresults.text)
if match.group() == pa:
report = "测试通过"
except AttributeError:
report = "测试失败"
return report # 正则表达式检测 @classmethod
def test_http(cls, code):
print "请求返回状态码: ", code @classmethod
def test_time(cls):
nowtime = datetime.today()
time = nowtime.strftime("%Y-%m-%d %H:%M:%S")
return time # 获取当前时间转化字符串 @classmethod
def test_report(cls):
nowtime = datetime.today()
reportime = nowtime.strftime("%Y%m%d%H%M%S")
reportname = reportime + ".xml"
return reportname # 获取测试报告文件名称 @classmethod
def test_main(cls):
global testresults
table = CreateExcel.open_excel()
nrows = CreateExcel.get_nrows(table)
xml = minidom.Document()
xml.appendChild(xml.createComment("测试报告"))
caselist = xml.createElement("caselist")
xml.appendChild(caselist)
for i in range(0, nrows - 1):
testid = CreateExcel.get_id(table, nrows)[i]
testname = CreateExcel.get_name(table, nrows)[i]
testdata = CreateExcel.get_data(table, nrows)[i]
testurl = CreateExcel.get_url(table, nrows)[i]
testmethod = CreateExcel.get_method(table, nrows)[i]
testpattern = CreateExcel.get_pattern(table, nrows)[i] # 执行测试
CreateTest.test_on()
testresults = CreateTest.test_api(testmethod, testurl, testdata)
testcode = str(testresults.status_code)
try:
CreateTest.test_http(testresults.status_code)
except AttributeError:
pass
CreateTest.test_close()
# 执行结束
# 生成xml文件
case = xml.createElement("case")
case.setAttribute("id", testid)
# 输入用例ID name = xml.createElement("name")
name.appendChild(xml.createTextNode(testname))
# 输入用例名称
method = xml.createElement("method")
method.appendChild(xml.createTextNode(testmethod))
# 输入接口类型
code = xml.createElement("code")
code.appendChild((xml.createTextNode(testcode)))
# 输入用例返回状态码
result = xml.createElement("result")
result.appendChild(xml.createTextNode(CreateTest.test_result(testpattern)))
# 输入用例测试结果
time = xml.createElement("time")
time.appendChild(xml.createTextNode(CreateTest.test_time()))
# 输入用例执行时间 case.appendChild(name)
case.appendChild(method)
case.appendChild(code)
case.appendChild(result)
case.appendChild(time) caselist.appendChild(case)
# xml文件生成结束
filename = file(CreateTest.test_report(), "w+")
# 生成以当前时间命名的测试报告文件
xml.writexml(filename)
filename.close()
# 关闭文件 if __name__ == '__main__':
CreateTest.test_main()

下面是测试入口:

#!/usr/bin/env python
# -*- coding: utf_8 -*-
# ****************************************************************
# interface.py
# Author : ChenLei
# Version : 2.0
# Date : 2016-4-15
# **************************************************************** import time
from createtest import CreateTest start = time.clock()
CreateTest.test_main()
end = time.clock() print "接口自动化脚本运行时间:%.03f seconds" % (end - start)

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

除非注明,本博客文章均为原创,转载请以链接形式标明本文地址

本文地址:http://www.cnblogs.com/cllovewxq/p/5394549.html

ython学习笔记(接口自动化框架 V2.0)的更多相关文章

  1. python学习笔记(接口自动化框架 V2.0)

    这个是根据上次框架版本进行的优化 用python获取excel文件中测试用例数据 通过requets测试接口.并使用正则表达式验证响应信息内容 生成xml文件测试报告 版本更新内容: 1. 整理了Cr ...

  2. 接口自动化 基于python+Testlink+Jenkins实现的接口自动化测试框架[V2.0改进版]

    基于python+Testlink+Jenkins实现的接口自动化测试框架[V2.0改进版]   by:授客 QQ:1033553122 由于篇幅问题,,暂且采用网盘分享的形式: 下载地址: [授客] ...

  3. python学习笔记(接口自动化框架 V1.0)

    之前是利用python自带的unittest测试框架 这次自己设计一个 之后再一点点往里面加功能 (ps:当然这个框架真的是很简单..很简单...很简单...) excel文件格式: #!/usr/b ...

  4. 接口自动化 [授客]基于python+Testlink+Jenkins实现的接口自动化测试框架V3.0

    基于python+Testlink+Jenkins实现的接口自动化测试框架V3.0   by:授客 QQ:1033553122     博客:http://blog.sina.com.cn/ishou ...

  5. 转载:python + requests实现的接口自动化框架详细教程

    转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实现的接口自动化框架详细教程 前段时间由于公司测试方向的转型,由 ...

  6. python + requests实现的接口自动化框架详细教程

    前段时间由于公司测试方向的转型,由原来的web页面功能测试转变成接口测试,之前大多都是手工进行,利用postman和jmeter进行的接口测试,后来,组内有人讲原先web自动化的测试框架移驾成接口的自 ...

  7. go语言,golang学习笔记2 web框架选择

    go语言,golang学习笔记2 web框架选择 用什么go web框架比较好呢?能不能推荐个中文资料多的web框架呢? beego框架用的人最多,中文资料最多 首页 - beego: 简约 & ...

  8. 并发编程学习笔记(15)----Executor框架的使用

    Executor执行已提交的 Runnable 任务的对象.此接口提供一种将任务提交与每个任务将如何运行的机制(包括线程使用的细节.调度等)分离开来的方法.通常使用 Executor 而不是显式地创建 ...

  9. mybatis学习笔记之基础框架(2)

    mybatis学习笔记之基础框架(2) mybatis是一个持久层的框架,是apache下的顶级项目. mybatis让程序将主要精力放在sql上,通过mybatis提供的映射方式,自由灵活生成满足s ...

随机推荐

  1. TypeScript 泛型及应用

    TypeScript 泛型及应用 一.泛型是什么 二.泛型接口 三.泛型类 四.泛型约束 4.1 确保属性存在 4.2 检查对象上的键是否存在 五.泛型参数默认类型 六.泛型条件类型 七.泛型工具类型 ...

  2. PTA 是否二叉搜索树 (25分)

    PTA 是否二叉搜索树 (25分) 本题要求实现函数,判断给定二叉树是否二叉搜索树. 函数接口定义: bool IsBST ( BinTree T ); 其中BinTree结构定义如下: typede ...

  3. Leetcode 课程表 C++ 图的深度搜索和广度搜索练习

    广度搜索(degree) struct GraphNode{ int label; vector<GraphNode*> neighbours; GraphNode(int x):labe ...

  4. 大爽Python入门教程 2-1 认识容器

    大爽Python入门公开课教案 点击查看教程总目录 1 什么是容器 先思考这样一个场景: 有五个学生,姓名分别为: Alan, Bruce, Carlos, David, Emma. 需要给他们都打一 ...

  5. [cf1479D]Odd Mineral Resource

    先考虑判定是否有解,注意到无解即每一个数都出现偶数次,根据异或的性质,只需要随机$V_{i}$,假设$u$到$v$路径上所有节点构成集合$S$,若$\bigoplus_{x\in S,l\le a_{ ...

  6. [tc14634]ExtremeSpanningTrees

    保序回归论文题 要求某一个边集为原图的最小生成树,这等价于非树边比所在环(指树上)的所有边小,最大生成树类似 将这些大小关系的限制看作一张有向图,即若要求$w_{i}\le w_{j}$则连边$(i, ...

  7. 【IDEA】颜色主题 Color Theme

    颜色主题 Color Theme 2020-09-08  08:35:44  by冲冲 1.本人的颜色主题:TasteTheRainbow.jar 链接:https://pan.baidu.com/s ...

  8. Android连接远程数据库的避坑指南

    Android连接远程数据库的避坑指南 今天用Android Studio连接数据库时候,写了个测试连接的按钮,然后连接的时候报错了,报错信息: 2021-09-07 22:45:20.433 705 ...

  9. CF1354F Summoning Minions

    考虑我们一定是先放我们选定了\(m\)个数,一定是先放了\(m-1\)个数上去,然后让放上一个不打算选的然后拿下来,白嫖\(b * (m-1)\)的贡献,最后放上一个打算放的. 考虑我们一定是按\(b ...

  10. Codeforces 1175G - Yet Another Partiton Problem(李超线段树)

    Codeforces 题面传送门 & 洛谷题面传送门 这是一道李超线段树的毒瘤题. 首先我们可以想到一个非常 trivial 的 DP:\(dp_{i,j}\)​ 表示前 \(i\)​ 个数划 ...