request库:request是来发送http请求的(用代码带起postman的操作)
unittest:做单元测试-----断言
unittest组成部分
unitest是python的自动化测试框架,unitest框架包含下列部分:
·TestCase(测试用例)
·TestSuite(测试套件,把多个TestCase集成到一个测试TestSuite)
·TestRunner(执行测试用例)
·TestLoader(自动从代码中加载多个测试用例)
·Fixure(UnitTest特性)
为什么使用unittest框架
·能够组织多个用例去执行
·提供丰富的断言方法
·能够生成测试报告(由于生成的测试报告简陋,我们选择其他插件实现)
TestCase
def my_sum(a,b):
a+=1 b+=2 return a+b
class TestSum(unittest.TestCase):
def test_01(self): print(my_sum(1,2))
def test_01(self): print(my_sum(1,2))
if __name__==__main__:
TestSum()
测试模板:
import requests
import json
def fun1():
URL = "http://localhost:8080/api/sys/login"
data = { "mobile": "13800000002", "password": "123456" }
json_data = json.dumps(data) deader = { "Content-type": "application/json" }
res = requests.post(url=URL, data=json_data, headers=deader)
res_data = res.json() token = res_data[‘data‘]
print(token) print(res_data)
new_token = ‘Bearer ‘ + token
new_deader = { "Authorization": new_token, "Content-type": "application/json" }
url = "http://localhost:8080/api/sys/user"
data = { "page": 1, "size": 10 }
res1 = requests.get(url=url, headers=new_deader, params=data)
print(res1)
res_data = res1.json()
print(res_data)
if __name__ == ‘__main__‘:
fun1()