Python+ddt模块做接口自动化login登录用例【接口自动化框架设计系列】【多测师】
# coding=utf-8 """ =========================== Author:多测师_王sir Time:2020/5/20 17:24 Wechat:15367499889 Company:上海多测师信息有限公司 =========================== """ import unittest import os from library.ddt import ddt, data from common.readexcel import ReadExcel from common.handlepath import DATADIR from common.handleconfig import conf from common.handlerequests import SendRequest from common.handlelog import log case_file = os.path.join(DATADIR, "apicases.xlsx") print(case_file) @ddt class TestLogin(unittest.TestCase): excel = ReadExcel(case_file, "login") #拿到login工作簿的对象 cases = excel.read_data() #拿到Excel表格中的用例、是一个列表里面有3个字典用例 request = SendRequest() #创建了一个session对象 @data(*cases) def test_login(self, case): # 第一步:准备用例数据 url = conf.get("env", "url") + case["url"] method = case["method"] # print(url) #url = http://192.168.1.192:8080/cms/manage/loginJump.do # print(method) #post data = eval(case["data"]) headers = eval(conf.get("env", "headers")) expected = eval(case["expected"]) row = case["case_id"] + 1 # 第二步:发送请求,获取结果 response = self.request.send(url=url, method=method, data=data, headers=headers) res = response.json() #通过json把响应的参数转换为字典格式 print(res) # 第三步:断言(比对预期结果和实际结果) try: self.assertEqual(expected["code"], res["code"]) self.assertEqual(expected["msg"], res["msg"]) except AssertionError as e: self.excel.write_data(row=row, column=8, value="未通过") log.error("用例:{},执行未通过".format(case["title"])) log.exception(e) raise e else: self.excel.write_data(row=row, column=8, value="通过") log.info("用例:{},执行通过".format(case["title"])) if __name__ == '__main__': unittest.main()