unittest管理接口用例
1.加入unittest框架
#coding=utf-8
import requests
import unittest
class TestApi(unittest.TestCase):
def setUp(self):
self.apiurl = "http://www.xxxxx.com/customer/login.html"
self.header = {"Cookie": "mediav=%7B%22eid%22%3A%22470884%22%2C%22ep%22%3A%22%22%2C%22vid%22%3A%22%40-vC_Waqh_%3AU%234K75o%5B!%22%2C%22ctn%22%3A%22%22%7D"}
self.timeout = 1
def testlogin01(self):
body = {"loginName":17779828888,"loginPwd":"zy123456"}
response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
":
pass
else:
raise ValueError
def testlogin02(self):
body = {"}
response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
if response.json()["errorMsg"] == u"用户或者密码错误":
pass
else:
raise ValueError
def testlogin03(self):
body = {"}
response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
if response.json()["errorMsg"] == u"用户或者密码错误":
pass
else:
raise ValueError
def testlogin04(self):
body = {"}
response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
if response.json()["errorMsg"] == u"用户名不能为空":
pass
else:
raise ValueError
def testlogin05(self):
body = {"loginName":17779828888,"loginPwd":""}
response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
if response.json()["errorMsg"] == u"用户密码不能为空":
pass
else:
raise ValueError
if __name__ == '__main__':
unttest.main()
2.生成测试报告
#coding=utf-8
import requests
import unittest
import HTMLTestRunner
class TestApi(unittest.TestCase):
def setUp(self):
self.apiurl = "http://www.xxxxx.com/customer/login.html"
self.header = {"Cookie": "mediav=%7B%22eid%22%3A%22470884%22%2C%22ep%22%3A%22%22%2C%22vid%22%3A%22%40-vC_Waqh_%3AU%234K75o%5B!%22%2C%22ctn%22%3A%22%22%7D"}
self.timeout = 1
def testlogin01(self):
body = {"loginName":17779828888,"loginPwd":"zy123456"}
response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
":
pass
else:
raise ValueError
def testlogin02(self):
body = {"}
response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
if response.json()["errorMsg"] == u"用户或者密码错误":
pass
else:
raise ValueError
def testlogin03(self):
body = {"}
response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
if response.json()["errorMsg"] == u"用户或者密码错误":
pass
else:
raise ValueError
def testlogin04(self):
body = {"}
response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
if response.json()["errorMsg"] == u"用户名不能为空":
pass
else:
raise ValueError
def testlogin05(self):
body = {"loginName":17779828888,"loginPwd":""}
response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
if response.json()["errorMsg"] == u"用户密码不能为空":
pass
else:
raise ValueError
if __name__ == '__main__':
suit = unittest.TestSuite()
testcases = [TestApi("testlogin01"),TestApi("testlogin02"),TestApi("testlogin03"),
TestApi("testlogin04"),TestApi("testlogin05")]
suit.addTests(testcases)
dir = "D:\\testapi.html"
path = open(dir,"wb")
runner = HTMLTestRunner.HTMLTestRunner(stream=path,title="TestReport",description="TestDesc")
runner.run(suit)
path.close()
3.断言
- if ... else ... 如上代码
- try ... except ...
#coding=utf-8
import requests
import unittest
class TestApi(unittest.TestCase):
def setUp(self):
self.apiurl = "http://www.xxxx.com/customer/login.html"
self.header = {"Cookie": "mediav=%7B%22eid%22%3A%22470884%22%2C%22ep%22%3A%22%22%2C%22vid%22%3A%22%40-vC_Waqh_%3AU%234K75o%5B!%22%2C%22ctn%22%3A%22%22%7D"}
self.timeout = 1
def testlogin01(self):
body = {"loginName":17779828888,"loginPwd":"zy295240???"}
response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
try:
result = response.json()["values"]["loginName"]
":
pass
else:
raise ValueError
except:
print ("testlogin01 error!")
else:
print ("testlogin01 ok!")
if __name__ == '__main__':
unittest.main()
- unttest 中 assert断言方式
#coding=utf-8
import requests
import unittestclass TestApi(unittest.TestCase):
def setUp(self):
self.apiurl = "http://www.xxxx.com/customer/login.html"
self.header = {"Cookie": "mediav=%7B%22eid%22%3A%22470884%22%2C%22ep%22%3A%22%22%2C%22vid%22%3A%22%40-vC_Waqh_%3AU%234K75o%5B!%22%2C%22ctn%22%3A%22%22%7D; "}
self.timeout = 1
def testlogin01(self):
body = {"loginName":17779828888,"loginPwd":"zy123456"}
response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
phone = response.json()["values"]["loginName"]
self.assertEqual(phone,",msg="testlogin01 error!")
def testlogin02(self):
body = {"loginName":17779828881,"loginPwd":"zy123456"}
response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
errorMsg = response.json()["errorMsg"]
self.assertNotEqual(errorMsg,u"成功",msg="testlogin02 error!")
def testlogin03(self):
body = {"}
response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
errorMsg = response.json()["errorMsg"]
self.assertIn(u"密码错误",errorMsg,msg="testlogin03 error!")
def testlogin04(self):
body = {"}
response = requests.post(url=self.apiurl,data=body,headers=self.header,timeout=self.timeout)
errorMsg = response.json()["errorMsg"]
self.assertNotIn(u"密码错误",errorMsg,msg="testlogin04 error!")
if __name__ == '__main__':
unittest.main()
unittest管理接口用例的更多相关文章
- unittest管理接口用例(数据分离-读取excel)
1.简单读取 #coding=utf-8 #调用封装好的excel读取公共方法 from python_API.common.ReadExcel import ReadExcel import req ...
- unittest 管理接口用例(数据分离-读取excel)
1.公共模块 ---> login.xls """ common (package) ---> ReadFile.py """ ...
- requests,unittest——多接口用例,以及需要先登录再发报的用例
之前写过最简单的接口测试用例,本次利用unittest进行用例管理,并出测试报告,前两个用例是两个不同网页的get用例,第三个是需要登录才能访问的网页A,并在其基础上访问一个需要在A页面点开链接才能访 ...
- python web自动化测试框架搭建(功能&接口)——接口用例实现
测试用例基类: # coding=utf-8 import unittest import Logger log = Logger.Loger() class BaseCase(unittest.Te ...
- python3+requests+unittest:接口自动化测试(一)
转载请表明出处:https://www.cnblogs.com/shapeL/p/9179484.html 简单介绍框架的实现逻辑,参考代码的git地址: https://github.com/zha ...
- python脚本实现接口自动化轻松搞定上千条接口用例
接口自动化目前是测试圈主流的一个话题,我也在网上搜索了很多关于自动化的关键词,大多数博主分享的python做接口自动化都是以开源的框架,比如:pytest.unittest+ddt(数据驱动) 最常见 ...
- python学习笔记(28)-unittest单元测试-执行用例
执行用例 #写一个测试类 import unittest import HTMLTestRunnerNew #写好的模块可以直接调用 #import HTMLTest #测试报告模板 from cla ...
- C#开发微信门户及应用(23)-微信小店商品管理接口的封装和测试
在上篇<C#开发微信门户及应用(22)-微信小店的开发和使用>里面介绍了一些微信小店的基础知识,以及对应的对象模型,本篇继续微信小店的主题,介绍其中API接口的封装和测试使用.微信小店的相 ...
- Lenovo System x3650 设置管理接口地址
1.开启服务器. 2.显示<F1> Setup提示后,按 F1.(此提示在屏幕上仅显示几秒钟.必须迅速按 F1.) 如果同时设置了开机密码和管理员密码,则必须输入管理员密码才能访问完整的 ...
随机推荐
- JSON字符串转实体对象
JSON转实体两种方式 代码片段 ; i < dt.Rows.Count; i++) { //Json字符串 string designJson = dt.Rows[i]["Desig ...
- Java 文件句柄泄露问题解决小记(转)
转:Java 文件句柄泄露问题解决小记 维护 WebIDE 免不了要管理很多的文件, 自从我们线上系统增加了资源回收功能,便一直受一个问题困扰:后台线程解绑目录时偶尔报错,看症状因为是某些文件被占用了 ...
- Android Studio运行Hello World程序
老的神舟本本装上了深度LINUX了...应该是基于ubuntu的,安装软件用的apt-get而不是yum 想重装学下android原生开发,官网下载了android studio, 发现不用FQ也能下 ...
- 最好用的Redis Desktop Manager 0.9.3 版本下载
因为Redis Desktop Manager作者在 0.9.4 版本之后选择对所有的安装包收费,不再提供安装包下载,但是源码依旧公开. github 上有 redis destop manager ...
- java导出pdf功能记录
这几天已在做处理导出pdf文件的功能,摸索了几天总算可以了.记录下这几天遇到的问题. 1.网上基本都是基于Itext5和Itext7来处理的.我最终是在Itext5上成功了,itext7应该是模板出问 ...
- 你该怎么学习C++——思想层面
Javascript是世界上最受误解的语言,其实C++何尝不是.坊间流传的错误的C++学习方法一抓就是一大把.我自己在学习C++的过程中也走了许多弯路,浪费了不少时间. 为什么会存在这么多错误认识?原 ...
- Gerrit - Gerrit与GitLab集成
1 - 简介 虽然Gerrit 本身提供 Code Review和 Git 仓库的两大功能,但实际上很多项目用的是其他的Git仓库,例如GitLab和GitHub. 一般情况下,Gerrit位于最终代 ...
- 一款新的好用的SSH工具——FinalShell
FinalShell是一体化的的服务器,网络管理软件,不仅是ssh客户端,还是功能强大的开发,运维工具,充分满足开发,运维需求.特色功能:免费海外服务器远程桌面加速,ssh加速,双边tcp加速,内网穿 ...
- 小甲鱼汇编语言学习笔记——day03
手动编译并执行第一个汇编程序过程: 1.用notepad++写一个简单的汇编程序(文件命名为:1.asm): assume cs:abc abc segment mov ax, 2 add ax, a ...
- 【LOJ2292】[THUSC2016]成绩单(区间DP)
题目 LOJ2292 分析 比较神奇的一个区间 DP ,我看了很多题解都没看懂,大约是我比较菜罢. 先明确一下题意:abcde 取完 c 后变成 abde ,可以取 bd 这样取 c 后新增的连续段. ...