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)的更多相关文章

  1. unittest管理接口用例(数据分离-读取excel)

    1.简单读取 #coding=utf-8 #调用封装好的excel读取公共方法 from python_API.common.ReadExcel import ReadExcel import req ...

  2. unittest管理接口用例

    1.加入unittest框架 #coding=utf-8 import requests import unittest class TestApi(unittest.TestCase): def s ...

  3. python接口自动化测试--数据分离读取Excal指定单元格数据

    上一篇博客讲了怎么批量读取Excal单元格数据,现在咱们说一下怎么读取Excal指定单元格数据. 一.首先建一个Test_Main类 #!/usr/bin/python # -*- coding: U ...

  4. Jmeter 接口自动化-脚本数据分离实例

    一. 背景:  为了让大家更加的了解Jmeter,并且使用起来游刃有余.这篇我们主要讲一下,如何优雅的使用Jmeter一步步的实现接口自动化,完成脚本与数据分离,把可能对Jmeter脚本的维护转移到c ...

  5. Python 用load_workbook 读取excel某个单元格数据、读取excel行数、列数

    from openpyxl import load_workbook path = r'D:\pywork\12' # EXCEL信息所在文件夹 e= load_workbook(path + '/' ...

  6. requests,unittest——多接口用例,以及需要先登录再发报的用例

    之前写过最简单的接口测试用例,本次利用unittest进行用例管理,并出测试报告,前两个用例是两个不同网页的get用例,第三个是需要登录才能访问的网页A,并在其基础上访问一个需要在A页面点开链接才能访 ...

  7. python web自动化测试框架搭建(功能&接口)——接口用例实现

    测试用例基类: # coding=utf-8 import unittest import Logger log = Logger.Loger() class BaseCase(unittest.Te ...

  8. C#读取Excel数据操作大全

    苦丁茶 发表于 2014-02-10 12:58:00 | 分类标签: ASP.NET 读取Excel 本文介绍下,用C#读取excel数据的例子,包括读取整个工作薄的数据.读取工作薄选定区域中的数据 ...

  9. C# 读取Excel中的数据

    #region 读取Excel中的数据 /// <summary> /// 读取Excel中的数据 /// </summary> /// <param name=&quo ...

随机推荐

  1. Django 配置访问顺序 ->MTV开发模式

    框架模式mvc m-->model 数据库 v-->view  视图 c-->controller  控件逻辑 mtv(django) m-->model 数据库 t--> ...

  2. Struts2-学习笔记系列(8)-异常处理

    后台抛出自定义异常 public String execute() throws Exception { if (getUser().equalsIgnoreCase("user" ...

  3. 微信小程序页面通信

    目录 微信小程序页面通信 方式一:通过URL 方式二:通过全局变量 方式三:通过本地存储 方式四:通过路由栈 微信小程序页面通信 方式一:通过URL // A 页面 wx.navigateTo({ u ...

  4. Linux C++ 网络编程学习系列(1)——端口复用实现

    Linux C++ 网络编程学习系列(1)--端口复用实现 源码地址:https://github.com/whuwzp/linuxc/tree/master/portreuse 源码说明: serv ...

  5. tf.nn.conv2d 卷积

    tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None) 第一个参数input:指需要做卷积的输入 ...

  6. hibernate.current_session_context_class 比较权威的解释

    hibernate.current_session_context_class 博客分类: hibernate HibernateSpring多线程配置管理thread  遇到过的问题: 情景1: 在 ...

  7. 数据结构和算法(Golang实现)(6)简单入门Golang-并发、协程和信道

    并发.协程和信道 Golang语言提供了go关键字,以及名为chan的数据类型,以及一些标准库的并发锁等,我们将会简单介绍一下并发的一些概念,然后学习这些Golang特征知识. 一.并发介绍 我们写程 ...

  8. Redis学习三:Redis高可用之哨兵模式

    申明 本文章首发自本人公众号:壹枝花算不算浪漫,如若转载请标明来源! 感兴趣的小伙伴可关注个人公众号:壹枝花算不算浪漫 22.jpg 前言 Redis 的 Sentinel 系统用于管理多个 Redi ...

  9. 时间格式的转化 vue与js 年月日 时分秒

    首先使用原生转化的方法 第一种 //时间转换 dateStr(d, sign) { //如果没有传递符号,给一个默认的符号 if (!sign) { sign = '-' } //获取d里面年月日时分 ...

  10. 作为python开发者,这几个PyCharm 技巧你必须掌握!

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取htt ...