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的依赖删除 ...
随机推荐
- 吴裕雄--天生自然 JAVA开发学习:运算符
public class Test { public static void main(String[] args) { int a = 10; int b = 20; int c = 25; int ...
- 黑马_13 Spring Boot:01.spring boot 介绍&&02.spring boot 入门
13 Spring Boot: 01.spring boot 介绍&&02.spring boot 入门 04.spring boot 配置文件 SpringBoot基础 1.1 原有 ...
- Necklace HDU - 3874 (线段树/树状数组 + 离线处理)
Necklace HDU - 3874 Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...
- SLAM资料
当下SLAM方案的总体介绍 http://wwwbuild.net/roboteasy/908066.html slam基础知识 https://www.zhihu.com/question/3518 ...
- 人工智能必备之Python3.8.1-安装
1_下载Python 2_下载Python 3_下载Python 4_下载Python-选这里下载:Windows x86-64 executable installer 5_安装Python 6.自 ...
- py学习笔记1.13、1.14
1.name.title() 首字母大写 name.upper() 全部大写 name.lower() 全部小写 2.+ 合并字符串 3.单引号.双引号都可以表示字符串 4.# 注释 5.索引制定为- ...
- rare alleles
I.4 Where the rare alleles are found p是基因A的频率,N是个体数目(也就是基因型个数,所以基因个数是2n,所以全部个体的基因A的个数是2np),p方是PAA,np ...
- Java中使用PrepateStatement并且like模糊查询
在使用PreparedStatement进行模糊查询的时候废了一番周折,以前一直都没有注意这个问题.一般情况下我们进行精确查询,sql语句类似:select * from table where na ...
- 共克时艰,停工不停学 Serverless 在线课堂免费开课
二月份,Serverless 团队联合腾讯云大学与云+社区免费推出 Serverless 在线直播课程,课程涵盖 Serverless 架构解密.技术解析以及不同应用场景下的最佳实战指导,让你从 0 ...
- springboot学习笔记:12.解决springboot打成可执行jar在linux上启动慢的问题
有时候,当你把你的springboot项目打成可执行的jar,放在linux上启动时,发现启动超级慢: 这往往是因为springboot内置tomcat启动时实例化SecureRandom对象随机数策 ...