# encoding=utf-8
import requests
import json
from jsonpath import jsonpath
class Testcase:
def selStus(self):
# 查询所有学生
# url = 'http://120.26.86.162:8090/students'
url = 'http://localhost:8090/students'
res = requests.get(url)
print(res.status_code)
print(res.json())
print('-------------------------')
#获取json返回值
print(jsonpath(res.json(), '$.code')[0])
print(jsonpath(res.json(), '$.msg')[0])
print(jsonpath(res.json(), '$.data')[0])
#获取json中的data对应的值
thisdict = jsonpath(res.json(), '$.data')[0]
print(len(thisdict))
itemvalue=["id", "studentId", "name", "sex","className", "courseName", "score", "email"]
for index in range(len(thisdict)):
print(thisdict[index])
for item0 in itemvalue:
print(thisdict[index][item0])
print('namevalue-------------------------')
print(res.text)
print('-------------------------')
def addstu(self):
# 新增学生
url = 'http://localhost:8090/studentAdd'
headers = {"Content-Type": "application/json", "Accept": "application/json"}
stuinfo = {
"className": "高三",
"courseName": "数学",
"email": "nuodashijie@163.com",
"id": 0,
"name": "六哥",
"score": 100,
"sex": "boy",
"studentId": "liuge01"}
res = requests.post(url, json=stuinfo, headers=headers)
print(res.status_code)
print(res.json())
#获取json返回值
print(jsonpath(res.json(), '$.code')[0])
print(jsonpath(res.json(), '$.msg')[0])
print(jsonpath(res.json(), '$.data')[0])
#获取json中的data对应的值
thisdict = jsonpath(res.json(), '$.data')[0]
print('thisdict-------------------------')
print(len(thisdict))
itemvalue=["id", "studentId", "name", "sex","className", "courseName", "score", "email"]
for item0 in itemvalue:
print(thisdict[item0])
print('-------------------------')
print(res.text)
print('-------------------------')
def studentUpateByid(self):
url='http://localhost:8090/studentUpdate/1?studentId=liuge01&name=%E5%85%AD%E5%93%A5&score=100&sex=boy&className=%E9%AB%98%E4%B8%89&courseName=%E6%95%B0%E5%AD%A6&email=nuodashijie%40163.com'
headers = {"Content-Type": "application/json", "Accept": "application/json"}
stuUPinfo = {
"className": "高三",
"courseName": "数学",
"email": "nuodashijie@163.com",
"id": 0,
"name": "六哥",
"score": 100,
"sex": "boy",
"studentId": "liuge01"}
res = requests.put(url, json=stuUPinfo, headers=headers)
print('studentUpateByid-------------------------')
print(res.status_code)
print(res.json())
#获取json返回值
print(jsonpath(res.json(), '$.code')[0])
print(jsonpath(res.json(), '$.msg')[0])
print(jsonpath(res.json(), '$.data')[0])
#获取json中的data对应的值
thisdict = jsonpath(res.json(), '$.data')[0]
print('thisdict-------------------------')
print(len(thisdict))
itemvalue=["id", "studentId", "name", "sex","className", "courseName", "score", "email"]
for item0 in itemvalue:
print(thisdict[item0])
print('-------------------------')
print(res.text)
print('-------------------------')
print(res.text)
print('studentUpateByid-------------------------')
def selestubyone(self):
url='http://localhost:8090/findStudentByName/%E5%85%AD%E5%93%A5'
headers='{‘Accept: application/json’}'
res=requests.get(url)
print('name/////-------------------------')
print(res.status_code)
print(res.json())
#获取json返回值
print(jsonpath(res.json(), '$.code')[0])
print(jsonpath(res.json(), '$.msg')[0])
print(jsonpath(res.json(), '$.data')[0])
#获取json中的data对应的值
thisdict = jsonpath(res.json(), '$.data')[0]
print('thisdict-------------------------')
print(len(thisdict))
itemvalue=["id", "studentId", "name", "sex","className", "courseName", "score", "email"]
for index in range(len(thisdict)):
print(thisdict[index])
for item0 in itemvalue:
print(thisdict[index][item0])
print('name/////-------------------------')
def selestubyscore(self):
url='http://localhost:8090/findStudentByScore/100'
headers='{‘Accept: application/json’}'
res=requests.get(url)
print('score/////-------------------------')
print(res.status_code)
print(res.json())
#获取json返回值
print(jsonpath(res.json(), '$.code')[0])
print(jsonpath(res.json(), '$.msg')[0])
print(jsonpath(res.json(), '$.data')[0])
#获取json中的data对应的值
thisdict = jsonpath(res.json(), '$.data')[0]
print('thisdict-------------------------')
print(len(thisdict))
itemvalue=["id", "studentId", "name", "sex","className", "courseName", "score", "email"]
for index in range(len(thisdict)):
print(thisdict[index])
for item0 in itemvalue:
print(thisdict[index][item0])
print('score/////-------------------------')
def selbystuID(self):
url = 'http://localhost:8090/findStudentByStudentId/liuge01'
res = requests.get(url)
print('StudentId/////-------------------------')
print(res.status_code)
print(res.json())
print('StudentId/////-------------------------')
# 获取json返回值
print(jsonpath(res.json(), '$.code')[0])
print(jsonpath(res.json(), '$.msg')[0])
print(jsonpath(res.json(), '$.data')[0])
# 获取json中的data对应的值
thisdict = jsonpath(res.json(), '$.data')[0]
print('thisdict-------------------------')
print(len(thisdict))
itemvalue = ["id", "studentId", "name", "sex", "className", "courseName", "score", "email"]
for index in range(len(thisdict)):
print(thisdict[index])
for item0 in itemvalue:
print(thisdict[index][item0])
def delstubyid(self):
url='http://localhost:8090/studentDelete/1'
res=requests.delete(url)
print('delstubyid/////-------------------------')
print(res.status_code)
print(res.json())
# 获取json返回值
print(jsonpath(res.json(), '$.code')[0])
print(jsonpath(res.json(), '$.msg')[0])
print(jsonpath(res.json(), '$.data')[0])
# 获取json中的data对应的值
thisdict = jsonpath(res.json(), '$.data')[0]
print('thisdict-------------------------')
print(len(thisdict))
itemvalue = ["id", "studentId", "name", "sex", "className", "courseName", "score", "email"]
for index in range(len(thisdict)):
print(thisdict[index])
for item0 in itemvalue:
print(thisdict[index][item0])
print('delstubyid/////-------------------------')
def tojsonstr(self):
# PYTHon对象转JSON字符串
data = {
"className": "高三",
"courseName": "数学",
"email": "nuodashijie@163.com",
"id": 0,
"name": "六哥",
"score": 100,
"sex": "boy",
"studentId": "liuge01"}
jsonstr = json.dump(data, indent=4)
print(jsonstr)
def JSONtoDICT(self):
# 字符串转字典
data = '{ "className": "高三","courseName": "数学","email": "nuodashijie@163.com","id": 0,"name": "六哥","score": 100,"sex": "boy","studentId": "liuge01"}'
dict0 = json.load(data)
print(dict0.type())
testcase = Testcase()
#testcase.addstu()
#testcase.selestubyone()
#testcase.selestubyscore()
testcase.selbystuID()
#testcase.studentUpateByid()
# testcase.delstubyid()
#testcase.selStus()