方法一:
---- coding:utf-8 ----
import requests
import json
from config import api_config
class Test_api(object):
def init(self):
self.token =' '
# 登录
def test_a_login(self):
# dumps将dict转化成str格式
result = requests.post(url=api_config.login_url, data=json.dumps(api_config.login_params),
headers=api_config.header_login)
print("result: " + result.text)
# 解析为json格式
jieguo = json.loads(result.text)
# 获取token
self.token = jieguo["results"]["token"]
# 首页
def test_b_homepage(self):
result = requests.get(url=api_config.home_url, headers={'service_name': 'businessservice','token':self.token})
print('test_homepage: '+ result.text)
if name == 'main':
testAPI = Test_api()
testAPI.test_a_login()
testAPI.test_b_homepage()
方法二:
---- coding:utf-8 ----
import requests
import unittest
import json
from config import api_config
from API.judegment import Judgment
from config import token
class Test_api(unittest.TestCase):
# 登录
def test_a_login(self):
# dumps将dict转化成str格式
result = requests.post(url=api_config.login_url, data=json.dumps(api_config.login_params),
headers=api_config.header_login)
self.assertEqual(result.status_code, 200, Judgment.status(result.status_code))
print("result: " + result.text)
# 解析为json格式
jieguo = json.loads(result.text)
# 获取token
token.token = jieguo["results"]["token"]
# 断言是否成功
self.assertEqual(jieguo['message'], '成功')
# 首页
def test_b_homepage(self):
result = requests.get(url=api_config.home_url, headers={'service_name': 'businessservice','token':token.token})
self.assertEqual(result.status_code, 200, Judgment.status(result.status_code))
print('test_homepage: '+ result.text)
if name == 'main':
unittest.main()
网友评论