unittest 管理接口用例(数据分离-读取excel)
1、公共模块
---> login.xls
"""
common (package)
---> ReadFile.py
""" import xlrd class ReadExcel():
def __init__(self,file_path,sheetx):
self.book = xlrd.open_workbook(file_path)
self.sheet = self.book.sheet_by_index(sheetx) def getValue(self,row_index,col_index):
"""
获取excel单元格数据
"""
return self.sheet.cell_value(row_index,col_index) def getCols(self):
"""
获取有效行数
"""
return self.sheet.ncols def getRows(self):
"""
获取有效列数
"""
return self.sheet.nrows
"""
common (package)
---> WriteFile.py
""" import xlrd from xlutils.copy import copy def WriteExcel(filepath,sheetx,rowx,colx,value):
"""
excel写入操作
"""
book = xlrd.open_workbook(filepath)
newbook = copy(book)
sheet = newbook.get_sheet(sheetx)
sheet.write(rowx,colx,value)
newbook.save(filepath)
"""
common (package)
---> var.py
"""
"""
excel用例模块等变量
"""
url = 2
Method = 3
header = 4
body = 5
test_response = 7
result = 8
ok = "成功"
fail = "失败"
2、输出testcase如下
import requests
import unittest
import HTMLTestRunner
import json
from API_test.common.ReadFile import ReadExcel
from API_test.common import var
from API_test.common.WriteFile import WriteExcel class TestLogin(unittest.TestCase): def setUp(self):
self.url = ReadExcel("D:\work_doc\CodeFile\API_test\common\login.xls", 0).getValue(1, var.url)
self.headers = json.loads(ReadExcel("D:\work_doc\CodeFile\API_test\common\login.xls", 0).getValue(1, var.header)) def test01(self):
"""
正向用例
"""
payload = json.loads(ReadExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0).getValue(1,var.body))
response = requests.request("POST", self.url, headers=self.headers, json = payload)
# 调用 WriteExcel 公共方法,将返回的报文实际结果写入到 excel 中
WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0,1,var.test_response,response.text)
# 断言
try:
self.assertEqual(response.json()["msg"],"成功",msg="test01 error")
except:
# 调用 WriteExcel 公共方法,将结论写入到 excel 中
WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0,1,var.result,var.fail)
# 再次断言生成测试报告,避免 try 异常处理将异常用例 pass 掉
self.assertEqual(response.json()["msg"], "成功", msg="test01 error")
else:
WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0,1,var.result,var.ok) def test02(self):
"""
账号错误
"""
payload = json.loads(ReadExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0).getValue(2,var.body))
response = requests.request("POST", self.url, headers=self.headers, json = payload)
WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0,2,var.test_response,response.text)
try:
self.assertEqual(response.json()["msg"],"用户不存在",msg="test02 error")
except:
WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls", 0, 2, var.result, var.fail)
self.assertEqual(response.json()["msg"],"用户不存在",msg="test02 error")
else:
WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0,2,var.result,var.ok) def test03(self):
"""
密码错误
"""
payload = json.loads(ReadExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0).getValue(3,var.body))
response = requests.request("POST", self.url, headers=self.headers, json = payload)
WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0,3,var.test_response, response.text)
try:
self.assertEqual(response.json()["msg"],"解密密码错误",msg="test03 error")
except:
WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls", 0, 3, var.result, var.fail)
self.assertEqual(response.json()["msg"],"解密密码错误",msg="test03 error")
else:
WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0,3,var.result,var.ok) if __name__ == '__main__':
suite = unittest.TestSuite()
testcase = [TestLogin("test01"),TestLogin("test02"),TestLogin("test03")]
suite.addTests(testcase)
reportfile = open("D:\work_doc\CodeFile\API_test\\report\\testreport.html", "wb")
runner = HTMLTestRunner.HTMLTestRunner(stream=reportfile,title="TestReport",description="测试结果")
runner.run(suite)
reportfile.close()
3、循环读取excel文件内的参数
from python_API.common.ReadExcel import ReadExcel
import requests
import json
import unittest class Test(unittest.TestCase): def setUp(self):
self.url = ReadExcel("d:\\dym.xls","Sheet1").getValue(1,1)
self.Method = ReadExcel("d:\\dym.xls","Sheet1").getValue(1,2)
self.header = json.loads(ReadExcel("d:\\dym.xls","Sheet1").getValue(1,3)) def test01(self): #调用读取excel类中的获取行数方法getRows(),获取有效行数
for row in range(ReadExcel("d:\\dym.xls","Sheet1").getRows()): #因为第一行为标题,所以row为0时不能用来取值
if row >=1:
body = json.loads(ReadExcel("d:\\dym.xls","Sheet1").getValue(row,4))
response = requests.request(self.Method,self.url,headers=self.header,params=body)
if row == 1:
if response.json()["executeStatus"] == 0:
pass
else:
print ("case01 error!")
else:
if response.json()["executeStatus"] == 1:
pass
else:
print ("case02 error!") if __name__ == '__main__':
unittest.main()
unittest 管理接口用例(数据分离-读取excel)的更多相关文章
- unittest管理接口用例(数据分离-读取excel)
1.简单读取 #coding=utf-8 #调用封装好的excel读取公共方法 from python_API.common.ReadExcel import ReadExcel import req ...
- unittest管理接口用例
1.加入unittest框架 #coding=utf-8 import requests import unittest class TestApi(unittest.TestCase): def s ...
- python接口自动化测试--数据分离读取Excal指定单元格数据
上一篇博客讲了怎么批量读取Excal单元格数据,现在咱们说一下怎么读取Excal指定单元格数据. 一.首先建一个Test_Main类 #!/usr/bin/python # -*- coding: U ...
- Jmeter 接口自动化-脚本数据分离实例
一. 背景: 为了让大家更加的了解Jmeter,并且使用起来游刃有余.这篇我们主要讲一下,如何优雅的使用Jmeter一步步的实现接口自动化,完成脚本与数据分离,把可能对Jmeter脚本的维护转移到c ...
- Python 用load_workbook 读取excel某个单元格数据、读取excel行数、列数
from openpyxl import load_workbook path = r'D:\pywork\12' # EXCEL信息所在文件夹 e= load_workbook(path + '/' ...
- requests,unittest——多接口用例,以及需要先登录再发报的用例
之前写过最简单的接口测试用例,本次利用unittest进行用例管理,并出测试报告,前两个用例是两个不同网页的get用例,第三个是需要登录才能访问的网页A,并在其基础上访问一个需要在A页面点开链接才能访 ...
- python web自动化测试框架搭建(功能&接口)——接口用例实现
测试用例基类: # coding=utf-8 import unittest import Logger log = Logger.Loger() class BaseCase(unittest.Te ...
- C#读取Excel数据操作大全
苦丁茶 发表于 2014-02-10 12:58:00 | 分类标签: ASP.NET 读取Excel 本文介绍下,用C#读取excel数据的例子,包括读取整个工作薄的数据.读取工作薄选定区域中的数据 ...
- C# 读取Excel中的数据
#region 读取Excel中的数据 /// <summary> /// 读取Excel中的数据 /// </summary> /// <param name=&quo ...
随机推荐
- java第九天,接口是什么?如何实现接口
接口 很多人纳闷Java为什么会有接口这个知识点呢?其实很大程度上是为了间接实现多继承.但是因为C++的多继承实在是一个难点,Java为了吸取C++的教训,就推出了接口这个概念.接口是一种公共规范标准 ...
- Docker+Jmeter+InfluxDB+Grafana搭建性能测试监控平台
搭建需求? jmeter自身的聚合测试报告可视化效果极差,为更加形象的.动态的展示测试过程,需要一个具有时序性的可视区来展示给我们的测试者, 这时候就需要用到后端监控,下面我们来开始搭建符合这种测试需 ...
- Java Array数组使用详解
本文主要讲解java中array数组使用,包含堆.栈内存分配及区别 1.动态初始化 package myArray; /* * 堆:存储的是new出来的东西,实体,对象 * A 每个对象都有地址值 * ...
- [算法]Huffman树(哈夫曼树)
目录 一.关于Huffman树 二.具体实现 例1:P1090 合并果子 例2:P2168 [NOI2015]荷马史诗 一.关于Huffman树 Huffman树(哈夫曼树)可以解决下述问题: 一颗\ ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十三)之Strings
Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation fo ...
- elasticsearch7.6.2实战(2)-es可视化及分析平台-kibana
1. 场景描述 elasticsearch部署完成后,es官方提供了可视化.分析及管理平台-kibana,部署下,有需要朋友参考下,不谢! 2. 解决方案 2.1 下载 (1)地址:https://w ...
- c语言中的引用使用
最近在写一个图像处理的程序时候,遇到一些传参的问题,最后发现引用的效率高一些,在此提醒各位道友,多多关注引用的应用及使用. 1.在引用的使用中,单纯给某个变量取个别名是毫无意义的,不要为了耍酷而乱用, ...
- 如何可视化深度学习网络中Attention层
前言 在训练深度学习模型时,常想一窥网络结构中的attention层权重分布,观察序列输入的哪些词或者词组合是网络比较care的.在小论文中主要研究了关于词性POS对输入序列的注意力机制.同时对比实验 ...
- Tensorflow 模型线上部署
获取源码,请移步笔者的github: tensorflow-serving-tutorial 由于python的灵活性和完备的生态库,使得其成为实现.验证ML算法的不二之选.但是工业界要将模型部署到生 ...
- 机器学习5- 对数几率回归+Python实现
目录 1. 对数几率回归 1.1 求解 ω 和 b 2. 对数几率回归进行垃圾邮件分类 2.1 垃圾邮件分类 2.2 模型评估 混淆举证 精度 交叉验证精度 准确率召回率 F1 度量 ROC AUC ...