requests-接口测试框架
一、风暴平台自动化接口测试
1.输出的各种值分别代表什么
2.登录使用post方法
3.个人主页
二.将以上代码改造为页面对象设计模式
2.1登录和个人主页
import requests import json import unittest def writeToken(content,fileName='token'): with open(fileName,'w') as f: f.write(content) def readToken(fileName='token'): with open(fileName,'r') as f: return f.read() class TestLogin(unittest.TestCase):#登录 def setUp(self) -> None:#初始化 r=requests.post( url='http://47.95.142.233:8000/login/auth/', #如果要用data的格式,就要对json进行序列化 data=json.dumps({"username":"13484545195","password":"asd888"}), headers={'Content-Type': 'application/json'}) writeToken(content=r.json()['token']) #? def tearDown(self) -> None:pass#清零 def test_platform_index(self):#个人主页 r=requests.get( url='http://47.95.142.233:8000/interface/index', headers={'Authorization':'JWT {token}'.format(token=readToken())}) self.assertEqual(r.status_code,200)#断言返回的协议状态码 self.assertEqual(r.json()['count']['product'],0)#断言json数据中返回的产品总数为0
2.2.添加产品(添加之后需要将产品id写入,在下个接口例如修改产品、删除产品当中调用)
2.3.查询产品(默认查询、模糊查询、精确查询)
2.4.修改产品(修改产品名称、类型、版本、负责人、描述)
def modifyName(self):#修改产品名称 r=requests.put( url='http://47.95.142.233:8000/interface/product/{productID}/'. format(productID=readToken(fileName='productID')), json={"name":"123","product_type":"WEB","version":"1.1","master":"limin","description":"aaa","id":5459}, headers={'Authorization':'JWT {token}'.format(token=readToken())}) return r def modifyType(self):#修改产品类型 r=requests.put( url='http://47.95.142.233:8000/interface/product/{productID}/'. format(productID=readToken(fileName='productID')), json={"name":"123","product_type":"APP","version":"1.1","master":"limin","description":"aaa","id":5466}, headers={'Authorization': 'JWT {token}'.format(token=readToken())}) return r def modifyVersion(self):#修改产品版本 r = requests.put( url='http://47.95.142.233:8000/interface/product/{productID}/'. format(productID=readToken(fileName='productID')), json={"name":"123","product_type":"APP","version":"2.1","master":"limin","description":"aaa","id":5466}, headers={'Authorization': 'JWT {token}'.format(token=readToken())}) return r def modifyOwner(self):#修改产品负责人 r = requests.put( url='http://47.95.142.233:8000/interface/product/{productID}/'. format(productID=readToken(fileName='productID')), json={"name":"123","product_type":"APP","version":"2.1","master":"李小敏","description":"aaa","id":5466}, headers={'Authorization': 'JWT {token}'.format(token=readToken())}) return r def modifyDescription(self):#修改产品描述 r=requests.put( url='http://47.95.142.233:8000/interface/product/{productID}/'. format(productID=readToken(fileName='productID')), json={"name":"123","product_type":"APP","version":"2.1","master":"李小敏","description":"bbb","id":5466}, headers={'Authorization': 'JWT {token}'.format(token=readToken())}) return r
2.5.删除产品
2.6.分别对添加产品、查询产品、修改产品、删除产品进行断言
def test_platform_add_product(self): """验证添加产品""" r=self.addProduct() print(r.json()) self.assertEqual(r.status_code,201) self.delProduct() def test_default_query_product(self): """验证默认查询""" r=self.defaultQuery() self.assertEqual(r.status_code,200) def test_fuzzy_query_product(self): """验证模糊查询""" r=self.fuzzyQuery() self.assertEqual(r.status_code,200) def test_exact_query_product(self): """验证精确查询""" r=self.exactQuery() self.assertEqual(r.status_code,200) def test_modify_name(self): """验证修改产品名称""" self.addProduct() r=self.modifyName() self.assertEqual(r.status_code, 200) self.assertEqual(r.json()["name"],"123") def test_modify_type(self): """验证修改产品类型""" self.addProduct() r=self.modifyType() self.assertEqual(r.status_code, 200) self.assertEqual(r.json()["product_type"],"APP") def test_modify_version(self): """验证修改产品版本""" self.addProduct() r=self.modifyVersion() self.assertEqual(r.status_code, 200) self.assertEqual(r.json()["version"],"2.1") def test_modify_owner(self): """验证修改产品负责人""" self.addProduct() r=self.modifyOwner() self.assertEqual(r.status_code, 200) self.assertEqual(r.json()["master"],"李小敏") def test_modify_description(self): """验证修改产品描述""" self.addProduct() r=self.modifyDescription() self.assertEqual(r.status_code, 200) self.assertEqual(r.json()["description"],"bbb") def test_platform_del_product(self): """验证删除产品""" self.addProduct() r=self.delProduct() self.assertEqual(r.status_code,204) if __name__ == '__main__': unittest.main()