unittest(6)- 作业- 测试类中写多个函数
实践作业:对多个接口发起请求,测试类中写多个测试函数
# 1. http_request
import requests
class HttpRequest:
def http_request(self, url, method, data=None, cookie=None):
try:
if method.upper() == "GET":
res = requests.get(url, data, cookies=cookie)
elif method.upper() == "POST":
res = requests.post(url, data, cookies=cookie)
else:
print("请输入正确的参数")
except Exception as e:
print("请求报错了:{}".format(e))
raise e
return res
if __name__ == '__main__':
# 登录
login_url = "http://v.rainbowred.com/login"
login_data = {"username": "15546355872",
"password": "123456",
"rememberCheck": "1",
"loginStatus": "1",
"rememberStatus": "1",
"autoLogin": "0",
"language": "zh"}
login_res = HttpRequest().http_request(login_url, "post", login_data)
print(login_res.json())
# 选择公司
c_url = "http://v.rainbowred.com/chc"
c_data = {"companyId": "21"}
c_res = HttpRequest().http_request(c_url, "post", c_data, cookie=login_res.cookies)
print(c_res.json())
# 2. get_data.py
class GetData:
Cookie = None # 存储cookie, 初始值为none
if __name__ == "__main__":
print(GetData.Cookie) # 获取属性值
print(hasattr(GetData, "Cookie")) # 判断是否有cookie属性
setattr(GetData, "Cookie", "n123") # 修改属性值
# print(GetData.Cookie)
print(getattr(GetData, "Cookie")) # 获取属性值
delattr(GetData, "Cookie") # 删除属性
print(hasattr(GetData, "Cookie"))
# 3.test_login.py
import unittest
from API_AUTO.tools.http_request import HttpRequest
from day_20191202.get_data import GetData
"""
1.登录接口:4个用例
正确登录、不输入账号、不输入密码、错误账号、错误密码
2.充值接口:
正常充值、不输入账号、不输入金额、充值错误的金额负数
"""
class TestLogin(unittest.TestCase):
def setUp(self):
print("开始执行用例")
# 登录URL和考勤URL,这个地方的URL和data是写死的值,所以不用在setUp中传参
self.loin_url = "https://www.ketangpai.com/UserApi/login"
self.kq_url = "https://www.ketangpai.com/SummaryApi/attence?courseid=MDAwMDAwMDAwOcqZWH37Np"
def tearDown(self):
print("用例执行完毕")
def test_success(self):
data = {"email": "1255811581@qq.com", "password": "huahua90!@", "remember": 0}
res = HttpRequest().http_request(self.loin_url, "post", data)
r = res.json()["info"]
print("1正常登录结果是", r)
if res.cookies: # 如果res.cookies有值,就更新Cookie的属性值
setattr(GetData, "Cookie", res.cookies)
try:
self.assertEqual(r, "success")
except AssertionError as e:
print("test_success's error is {}".format(e))
raise e # 处理了异常后再抛出去
def test_account(self):
data2 = {"email": "", "password": "huahua90!@", "remember": 0}
res = HttpRequest().http_request(self.loin_url, "post", data2)
r = res.json()["info"]
print("2空用户名登录结果是", r)
try:
self.assertEqual(r, "用户名不能为空")
except AssertionError as e:
print("test_account's error is {}".format(e))
raise e
def test_pwd(self):
data3 = {"email": "1255811581@qq.com", "password": "", "remember": 0}
res = HttpRequest().http_request(self.loin_url, "post", data3)
r = res.json()["info"]
print("3空密码登录结果是", r)
try:
self.assertEqual(r, "密码不能为空")
except AssertionError as e:
print("test_pwd's error is {}".format(e))
raise e
def test_waccount(self):
data4 = {"email": "12558115810@qq.com", "password": "huahua90!@", "remember": 0}
res = HttpRequest().http_request(self.loin_url, "post", data4)
r = res.json()["info"]
print("4错误用户名登录结果是", r)
try:
self.assertEqual(r, "用户不存在")
except AssertionError as e:
print("test_waccount's error is {}".format(e))
raise e
def test_wpwd(self):
data5 = {"email": "1255811581@qq.com", "password": "huahua90!@111", "remember": 0}
res = HttpRequest().http_request(self.loin_url, "post", data5)
r = res.json()["info"]
print("5错误密码登录结果是", r)
try:
self.assertEqual(r, "密码错误,你不能再尝试了", "这个用例会失败")
except AssertionError as e:
print("test_wpwd's error is {}".format(e))
raise e
def test_kq(self):
data = {}
res = HttpRequest().http_request(self.kq_url, "get", data, getattr(GetData, "Cookie"))
# r = res.json()
print("6考勤记录是", res)
# self.assertEqual(res.)
if __name__ == "__main__":
unittest.main()
# 4.run.py # 自行补充完整
unittest(6)- 作业- 测试类中写多个函数的更多相关文章
- why pure virtual function has definition 为什么可以在基类中实现纯虚函数
看了会音频,无意搜到一个frameworks/base/include/utils/Flattenable.h : virtual ~Flattenable() = 0; 所以查了下“纯虚函数定义实现 ...
- 在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static
在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static ! 在C语言中,我们使用pthread_create ...
- 基类中定义的虚函数在派生类中重新定义时,其函数原型,包括返回类型、函数名、参数个数、参数类型及参数的先后顺序,都必须与基类中的原型完全相同 but------> 可以返回派生类对象的引用或指针
您查询的关键词是:c++primer习题15.25 以下是该网页在北京时间 2016年07月15日 02:57:08 的快照: 如果打开速度慢,可以尝试快速版:如果想更新或删除快照,可以投诉快照. ...
- 测试框架Unitest的运行原理,以及多个测试类中的执行顺序以及简化方法
单元测试单元测试(unit testing)是指对软件中的最小可测试单元进行检查和验证.对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体含义,如C语言中单元指一个函数,Java里单元指一 ...
- python定义类()中写object和不写的区别
这里需要说明一下: python3中,类定义默认继承object,所以写不写没有区别 但在python2中,并不是这样 所以此内容是针对python2的,当然python3默认继承,不代表我们就傻乎乎 ...
- unittest对单个测试类的多种测试执行方法总结
基于unittest测试框架编写的测试脚本,一般单个测试类下会有多个测试方法,unittest也提供多种测试执行方式,下面就不同方式或者需求一一实操并说明: 一.使用unittest下main()方法 ...
- 创建一个抽象的员工类, 抽象开发累继承员工类,JavaEE ,和安卓继承开发类在测试类中进行测试
/* 1 定义一个员工类 所有的子类都抽取(抽象类) Employee 属性:姓名 工号(生成get set ) 方法:工作 抽象 2 定义一个研 ...
- Junit测试类中如何调用Http通信
在使用Junit做测试的时候,有时候需要调用Http通信,无论是request还是response或者是session会话,那么在测试类里该如何调用呢,其实很简单,spring给我们提供了三个类 or ...
- 阶段3 1.Mybatis_03.自定义Mybatis框架_3.自定义mybatis的编码-根据测试类中缺少的创建接口和类
先认识一下这几个类.Resources是一个class SqlSessionFactoryBuilder 创建新项目 复制相关的依赖 复制之前的代码 复制到当前项目的src下 把Mybits的依赖删除 ...
随机推荐
- python语法基础-并发编程-进程-进程池以及回调函数
############### 进程池 ############## """ 进程池的概念 为什么会有进程池? 1,因为每次开启一个进程,都需要创建一个内存空间 ...
- ZJNU 1125 - A == B ?——中级
处理后再判断即可,处理过程注意考虑全面. /* Written By. StelaYuri */ #include<iostream> #include<string> usi ...
- ubuntu16cuda8 cuda9.0 共存
https://blog.csdn.net/lovebyz/article/details/80704800 转载
- 使用tensorflow的retrain.py训练图片分类器
参考 https://hackernoon.com/creating-insanely-fast-image-classifiers-with-mobilenet-in-tensorflow-f030 ...
- 吴裕雄--天生自然 PYTHON3开发学习:字典
dict = {'} dict1 = { 'abc': 456 } dict2 = { 'abc': 123, 98.6: 37 } dict = {'Name': 'Runoob', 'Age': ...
- 形参和实参|默认值|可选实参|tuple|*tuple|args|*args | **kwargs|args[:]|
#!/usr/bin/python def hello(i,greet='long time to see!'): out = "hello "+i+" "+g ...
- vs strcore.cpp(156) 内存泄漏
在一个静态函数的线程回调中,一个cstring的对象没释放. 遇到这种问题: 1.查看所有的指针对象,有没有合适的回收 2.查看代码的malloc,看看有没有free 3.一点一点注释代码,定位位置
- 二、linux-mysql -cmake方式安装mysql 5.5
1.安装解压cmake包 cmake软件cd /home/oldboy/tools/tar xf cmake-2.8.8.tar.gzcd cmake-2.8.8./configure#CMake ...
- 一个简单的jQuery回调函数例子
jQuery回调函数简单使用 比如说,我们想要点击某个按钮后触发事件, 先把一些指定内容给隐藏掉, 然后跳出相关信息的对话框. 如果使用普通的方法, 不用回调函数的话, 会有怎么样的效果呢? 效果是先 ...
- Servlet&JSP复习笔记 04
1.状态管理 因为HTTP协议是无状态协议,但很多时候需要将客户端和服务端的多次请求当做一个来对待.将多次交互中设计的数据进行保存. 状态:数据 管理:对数据的维护 2.Cookie 客户端向服务器发 ...