python学习笔记(接口自动化框架 V1.0)
2016-04-12 11:03
771 查看
之前是利用python自带的unittest测试框架
这次自己设计一个 之后再一点点往里面加功能
(ps:当然这个框架真的是很简单。。很简单。。。很简单。。。)
excel文件格式:
上面代码是处理excel文档的
下面代码是测试平台
这次自己设计一个 之后再一点点往里面加功能
(ps:当然这个框架真的是很简单。。很简单。。。很简单。。。)
excel文件格式:
#!/usr/bin/env python # -*- coding: utf_8 -*- 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_name(cls, table, nrows): testname = [] for i in range(1, nrows): testname.append(table.cell(i, 0).value) return testname # 获取用例name @classmethod def get_data(cls, table, nrows): testdata = [] for i in range(1, nrows): data = json.loads(table.cell(i, 1).value) testdata.append(data) return testdata # 获取data接口参数 @classmethod def get_url(cls, table, nrows): testurl = [] for i in range(1, nrows): testurl.append(table.cell(i, 2).value) return testurl # 获取接口测试url @classmethod def get_method(cls, table, nrows): testmethod = [] for i in range(1, nrows): testmethod.append(table.cell(i, 3).value) return testmethod # 获取接口测试method @classmethod def get_pattern(cls, table, nrows): testpattern = [] for i in range(1, nrows): testpattern.append(table.cell(i, 4).value) return testpattern # 获取接口期望响应结果 @classmethod def get_report(cls, table, nrows): testreport = [] for i in range(1, nrows): testreport.append(table.cell(i, 5).value) return testreport # 获取用例期望的运行结果 if __name__ == "__main__": CreateExcel()
上面代码是处理excel文档的
下面代码是测试平台
#!/usr/bin/env python # -*- coding: utf_8 -*- import requests import re from createexcel import CreateExcel class CreateTest: def __init__(self): pass @classmethod def test_api(cls, method, url, data): global results if method == "post": results = requests.post(url, data) if method == "get": results = requests.get(url, data) return results @classmethod def test_on(cls): print "用例执行开始" @classmethod def test_close(cls): print "用例执行结束" @classmethod def test_result(cls, ra, rb): if ra == rb: print "测试结果: 测试通过" else: print "测试结果: 测试失败" @classmethod def test_http(cls, code): if code == 200: print "测试请求: 请求通过" @classmethod def test_main(cls): global report table = CreateExcel.open_excel() nrows = CreateExcel.get_nrows(table) for i in range(0, nrows - 1): 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] testreport = CreateExcel.get_report(table, nrows)[i] CreateTest.test_on() print "测试用例:", testname try: testresults = CreateTest.test_api(testmethod, testurl, testdata) CreateTest.test_http(testresults.status_code) pattern = re.compile(testpattern) match = pattern.search(testresults.url) if match.group() == testpattern: report = "pass" CreateTest.test_result(testreport, report) except AttributeError: report = "no" CreateTest.test_result(testreport, report) except Exception.__base__: print "测试请求: 请求失败" report = None CreateTest.test_result(testreport, report) CreateTest.test_close() if __name__ == '__main__': CreateTest()
相关文章推荐
- 据廖雪峰python3教程----python学习第十二天
- Python lambda
- Python dict sort
- Python面向对象特性,走向类
- Python3.5爬虫入门示例-获取百度首页及相关的html页面
- 用Python写冒泡排序代码
- Python sort sorted OrderDict
- Python set方法总结
- Python collections deque - double-ended queue ()
- Selenium2 Python 自动化测试实战学习笔记(二)
- Python unittest中常用到的assert
- Python装饰器
- Python基础 变量和数据类型
- PhantomJS 与python的结合
- Python与自然语言处理(二)基于Gensim的Word2Vec
- Python基础:文件读写
- 【python学习】多线程编程
- python简史
- python面对对象编程------3:写集合类的三种方法
- 简单Python3爬虫程序(5)进阶:知乎网的登录与用户相关信息爬取