1.通过类变量获取token
import jsonpath
import pytest
import requests
class TestCase:
token = None
openid = None
sjproductid = None
userid = None
sjcartid = None
def test_login(self):
url = 'http://39.98.138.157:5000/api/login'
data = {"password": "123456","username": "admin"}
res = requests.post(url,json=data)
print(res.json())
sjmsg = jsonpath.jsonpath(res.json(),'$.msg')[0]
TestCase.token = jsonpath.jsonpath(res.json(),'$.token')[0]
assert 'success' == sjmsg
def test_getUserinfo(self):
url = 'http://39.98.138.157:5000/api/getuserinfo'
header = {"token":TestCase.token}
res = requests.get(url,headers = header)
TestCase.openid = jsonpath.jsonpath(res.json(),'$..openid')[0]
TestCase.userid = jsonpath.jsonpath(res.json(),'$..userid')[0]
print('userid',TestCase.userid)
print('openid',TestCase.openid)
print(res.json())
sjname = jsonpath.jsonpath(res.json(),'$..nikename')[0]
assert '风清扬' == sjname
def test_shopping(self):
url = 'http://39.98.138.157:5000/api/getproductinfo?productid=8888'
res = requests.get(url)
print(res.json())
TestCase.sjproductid = jsonpath.jsonpath(res.json(),'$..productid')[0]
assert 8888 == TestCase.sjproductid
def test_cart(self):
url = 'http://39.98.138.157:5000/api/addcart'
header = {'token':TestCase.token}
data = {"openid": TestCase.openid,"productid": TestCase.sjproductid,"userid": TestCase.userid}
res = requests.post(url,json = data,headers = header)
print(res.json())
excartid = 45233
TestCase.sjcartid = jsonpath.jsonpath(res.json(),'$..cartid')[0]
print(TestCase.sjcartid)
assert excartid == TestCase.sjcartid
def test_order(self):
url = 'http://39.98.138.157:5000/api/createorder'
header = {'token':TestCase.token}
data = {"cartid": TestCase.sjcartid, "openid": TestCase.openid, "productid": TestCase.sjproductid, "userid": TestCase.userid}
res = requests.post(url,json=data,headers = header)
print(res.json())
if __name__ == '__main__':
pytest.main(['-sv','test_demo1.py'])
2.通过返回值获取token
import jsonpath
import pytest
import requests
class TestCase:
def test_login(self):
url = 'http://39.98.138.157:5000/api/login'
data = {"password": "123456","username": "admin"}
res = requests.post(url,json=data)
print(res.json())
sjmsg = jsonpath.jsonpath(res.json(),'$.msg')[0]
token = jsonpath.jsonpath(res.json(),'$.token')[0]
assert 'success' == sjmsg
return token
def test_getUserinfo(self):
url = 'http://39.98.138.157:5000/api/getuserinfo'
header = {"token":TestCase().test_login()}
res = requests.get(url,headers = header)
TestCase.openid = jsonpath.jsonpath(res.json(),'$..openid')[0]
TestCase.userid = jsonpath.jsonpath(res.json(),'$..userid')[0]
print('userid',TestCase.userid)
print('openid',TestCase.openid)
print(res.json())
sjname = jsonpath.jsonpath(res.json(),'$..nikename')[0]
assert '风清扬' == sjname
if __name__ == '__main__':
pytest.main(['-sv','test_demo2.py'])
3.把token放在session里
import jsonpath
import pytest
import requests
class TestCase:
session = requests.session()
openid = None
sjproductid = None
userid = None
sjcartid = None
def test_login(self):
url = 'http://39.98.138.157:5000/api/login'
data = {"password": "123456","username": "admin"}
res=TestCase.session.post(url,json=data)
print(res.json())
sjmsg = jsonpath.jsonpath(res.json(),'$.msg')[0]
token = jsonpath.jsonpath(res.json(),'$.token')[0]
TestCase.session.headers.update({'token':token})
assert 'success' == sjmsg
def test_getUserinfo(self):
url = 'http://39.98.138.157:5000/api/getuserinfo'
res = TestCase.session.get(url)
TestCase.openid = jsonpath.jsonpath(res.json(),'$..openid')[0]
TestCase.userid = jsonpath.jsonpath(res.json(),'$..userid')[0]
print('userid',TestCase.userid)
print('openid',TestCase.openid)
print(res.json())
sjname = jsonpath.jsonpath(res.json(),'$..nikename')[0]
assert '风清扬' == sjname
def test_shopping(self):
url = 'http://39.98.138.157:5000/api/getproductinfo?productid=8888'
res = requests.get(url)
print(res.json())
TestCase.sjproductid = jsonpath.jsonpath(res.json(),'$..productid')[0]
assert 8888 == TestCase.sjproductid
def test_cart(self):
url = 'http://39.98.138.157:5000/api/addcart'
data = {"openid": TestCase.openid,"productid": TestCase.sjproductid,"userid": TestCase.userid}
res = TestCase.session.post(url,json=data)
print(res.json())
excartid = 45233
TestCase.sjcartid = jsonpath.jsonpath(res.json(),'$..cartid')[0]
print(TestCase.sjcartid)
assert excartid == TestCase.sjcartid
def test_order(self):
url = 'http://39.98.138.157:5000/api/createorder'
data = {"cartid": TestCase.sjcartid, "openid": TestCase.openid, "productid": TestCase.sjproductid, "userid": TestCase.userid}
res = TestCase.session.post(url,json=data)
print(res.json())
if __name__ == '__main__':
pytest.main(['-sv','test_demo3.py'])
4.封装请求
import requests
class HttpClient:
def __init__(self):
self.session = requests.Session()
def send_request(self,method,url,param_type=None,data=None,headers = None,**kwargs):
method = method.upper()
param_type = param_type.upper()
print(method)
if method == 'GET':
response = self.session.request(method=method,url=url,params = data,**kwargs)
elif method == 'POST':
if param_type == 'FORM':
response = self.session.request(method = method,url = url,data=data,**kwargs)
else:
response = self.session.request(method = method,url = url,json=data,**kwargs)
pass
elif method == 'DELETE':
if param_type == 'FORM':
response = self.session.request(method = method,url = url,data=data,**kwargs)
else:
response = self.session.request(method = method,url = url,json=data,**kwargs)
elif method == 'PUT':
if param_type == 'FORM':
response = self.session.request(method = method,url = url,data=data,**kwargs)
else:
response = self.session.request(method = method,url = url,json=data,**kwargs)
else:
raise ValueError
return response
def __call__(self,method,url,param_type,data=None,headers = None,**kwargs):
return self.send_request(method,url,param_type,data,headers = None,**kwargs)
def close_session(self):
self.session.close()
import allure
import jsonpath
import pytest
import requests
from class40.httpclient import HttpClient
@allure.feature('接口自动化测试')
class TestCase:
httpclient = HttpClient()
openid = None
sjproductid = None
userid = None
sjcartid = None
@allure.story('登录流程')
@allure.title('登录')
def test_login(self):
url = 'http://39.98.138.157:5000/api/login'
data = {"password": "123456","username": "admin"}
res=TestCase.httpclient(method = 'post',url = url,param_type='json',data=data )
print(res.json())
sjmsg = jsonpath.jsonpath(res.json(),'$.msg')[0]
token = jsonpath.jsonpath(res.json(),'$.token')[0]
TestCase.httpclient.session.headers.update({'token':token})
assert 'success' == sjmsg
@allure.story('获得个人信息的流程')
@allure.title('个人信息')
def test_getUserinfo(self):
url = 'http://39.98.138.157:5000/api/getuserinfo'
res = TestCase.httpclient(url = url,method = 'get',param_type='json')
TestCase.openid = jsonpath.jsonpath(res.json(),'$..openid')[0]
TestCase.userid = jsonpath.jsonpath(res.json(),'$..userid')[0]
print('userid',TestCase.userid)
print('openid',TestCase.openid)
print(res.json())
sjname = jsonpath.jsonpath(res.json(),'$..nikename')[0]
assert '风清扬' == sjname
@allure.story('购物流程')
@allure.title('购物')
def test_shopping(self):
url = 'http://39.98.138.157:5000/api/getproductinfo?productid=8888'
res = TestCase.httpclient(url = url,method = 'get',param_type='json')
print(res.json())
TestCase.sjproductid = jsonpath.jsonpath(res.json(),'$..productid')[0]
assert 8888 == TestCase.sjproductid
@allure.story('添加购物车流程')
@allure.title('添加购物车')
def test_cart(self):
url = 'http://39.98.138.157:5000/api/addcart'
data = {"openid": TestCase.openid,"productid": TestCase.sjproductid,"userid": TestCase.userid}
res = TestCase.httpclient(method = 'post',url = url,param_type='json',data=data)
print(res.json())
excartid = 45233
TestCase.sjcartid = jsonpath.jsonpath(res.json(),'$..cartid')[0]
print(TestCase.sjcartid)
assert excartid == TestCase.sjcartid
@allure.story('下单流程')
@allure.title('下单')
def test_order(self):
url = 'http://39.98.138.157:5000/api/createorder'
data = {"cartid": TestCase.sjcartid, "openid": TestCase.openid, "productid": TestCase.sjproductid, "userid": TestCase.userid}
res = TestCase.httpclient(method = 'post',url = url,param_type='json',data=data)
print(res.json())
if __name__ == '__main__':
pytest.main(['-sv','test_demo4.py'])
5.运行命令
import os
import pytest
if __name__ == '__main__':
pytest.main(['-sv','test_demo4.py','--alluredir','./result','--clean-alluredir'])
os.system('allure generate ./result -o ./reports --clean')