def get_session():
req = requests.session()
url = ""
data = {}
req.post(url, data, verify=False)
return req
class ParametrizedTestCase(unittest.TestCase):
def __init__(self, methodName='runTest', param=None):
super(ParametrizedTestCase, self).__init__(methodName)
@classmethod
def setUpClass(cls):
cls.rq = get_session() # 登录后的session
pass
class ApiTest(ParametrizedTestCase):
def test_api(self):
ls = read_excel(Element.API_FILE)
# self.req 就是得到了登录后的信息session
Config().config_req(ls,self.req)
@classmethod
def setUpClass(cls):
super(ApiTest, cls).setUpClass()
def __param_format(self, key):
param_type = {
str: lambda: str(uuid.uuid1()),
list: lambda: [],
dict: lambda: {}
}
return param_type[type(key)]()
{"id":11,"code":2222}
检查点可以设置为:{"code":22}
{"code":1,data:[{"id": "123"}, {}]}
检查点可以设置为:{"id":123}
def __check(self, hope, res):
hope = json.loads(hope)
fact = json.loads(res)
for items in fact:
if type(fact[items]) == list:
for item in fact[items]:
for k in hope:
if item.get(k, "") == hope[k]:
return "通过"
if type(fact[items]) == dict:
for k in hope:
if fact[items].get(k, "") == hope[k]:
return "通过"
for k in hope:
if fact.get(k, "") == hope[k]:
return "通过"
return "失败"