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

用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文件 如下:

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

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

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

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

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

  3. python学习笔记之——unittest框架

    unittest是python自带的单元测试框架,尽管其主要是为单元测试服务的,但我们也可以用它来做UI自动化测试和接口的自动化测试. unittest框架为我们编写用例提供了如下的能力 定义用例的能 ...

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

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

  5. Python学习笔记_04:Django框架简介

    目录 1 什么是Django? 2 Django框架的开发环境搭建 3 Django操作MySql数据库简介 4 功能强大的Django管理工具应用 1 什么是Django? Django是应用于We ...

  6. python+requests+excel 接口自动化框架

    一.项目框架如图: 1.common :这个包都是一些公共的方法,如:手机号加解密,get/post接口请求的方法封装,接口鉴权,发邮件,读写excel文件方法等等 2.result:存放每次运行的l ...

  7. webdriver(python)学习笔记七——多层框架定位与智能等待

    多层框架或窗口定位: switch_to_frame() switch_to_window() 智能等待: implicitly_wait() 现在web应用中经常会遇到框架如(frame)或窗口(w ...

  8. Python学习笔记:Unittest框架了解

    Unittest单元测试框架不仅可以适用于单元测试,还可以适用于自动化测试用来的开发与执行,该测试框架可执行测试用例,并提供丰富的断言方法,最终生成测试报告. 一.Unittest常用方法 1.Tes ...

  9. 纯python自研接口自动化脚本更新版本,让小白也能实现0到1万+的接口自动化用例

    查看完整文章点击原文链接:纯python自研接口自动化脚本更新版本,让小白也能实现0到1万+的接口自动化用例 你是否还在用postman\jmeter做接口自动化吗?用python的开源框架[unit ...

随机推荐

  1. wire_format.cc:1091] String field 'accountid' contains invalid UTF-8 data when serializing a protocol buffer. Use the 'bytes' type if you intend to send raw bytes.

    原因: 在protobuf 的string字段中存在中文,序列化的时候会出现截断数据,string这个类型带有检查功能 解决方法: 把protobuf中存在中文的string字段类型 改为bytes ...

  2. WebBench----简洁优美的网站压力测试工具

    [root@c webbench]# webbench -c 10 -t 20 http://www.baidu.com/Webbench - Simple Web Benchmark 1.5Copy ...

  3. 命名空间 <iostream>和<iostream.h> 由程序设计者命名的内存区域

    namespace_百度百科 https://baike.baidu.com/item/namespace/1700121?fromtitle=命名空间 namespace即“命名空间”,也称“名称空 ...

  4. mongoose学习

    #mongoose的使用详解# ##1.Scheme.model和Entity## Schema : 一种以文件形式存储的数据库模型骨架,不具备数据库的操作能力 Model : 由Schema发布生成 ...

  5. Pandas常用操作方法

    Pandas pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的. Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具. pandas提 ...

  6. 004-shiro简介

    一.什么是shiro shiro是apache的一个开源框架,是一个权限管理的框架,实现 用户认证.用户授权. spring中有spring security (原名Acegi),是一个权限框架,它和 ...

  7. DateTimeTypeHandler

    mysql-timestimp-------model-DateTime(jodatime) public class DataTimeTypeHandler extends BaseTypeHand ...

  8. 玩玩nmap

    ---恢复内容开始--- [root@miyan ~]# nmap -v Starting Nmap 7.12 ( https://nmap.org ) at 2016-04-04 15:34 CST ...

  9. python读取/写入文件

    <Python编程:从入门到实践>读书笔记 1.读取文件并且对文件内容进行打印有三种方式: with open('test.txt') as fo: for lins in fo: pri ...

  10. HadoopHA简述

    1 概述 在hadoop2.0之前,namenode只有一个,存在单点问题(虽然hadoop1.0有 secondarynamenode,checkpointnode,buckcupnode这些,但是 ...