自己尝试拿自己测试的系统编写接口自动化,不断优化,不断完善,记录学习过程,以及过程中遇到的问题及解决办法。

采用的结构是python+unittest,数据驱动模式

代码的结构:

接口自动化1.0 python+unittest

 测试数据:

接口自动化1.0 python+unittest

 处理测试数据,使用tool里面的get_data.py,代码:

import openpyxlclass Do_Excel:    def __init__(self, file_name, sheet_name):        self.file_name = file_name        self.sheet_name = sheet_name    def get_data(self):        wb = openpyxl.load_workbook(self.file_name)        sheet = wb[self.sheet_name]        test_data = []        list_data = []        for j in range(1, sheet.max_column + 1):            for i in range(1, sheet.max_row + 1):                list_data.append(sheet.cell(j, i).value)            test_data.append(list_data)            list_data = []        res = [dict(zip(test_data[0], test_data[i])) for i in range(1, len(test_data))]        return resif __name__ == ‘__main__‘:    res = Do_Excel(‘test_data.xlsx‘, ‘python‘).get_data()    print(res[0][‘url‘])测试用例test_cases下的test_formcenter.py 代码:
import unittestimport requestsfrom gongdan.tools.get_data import Do_Excelfrom gongdan import loginclass TestForm(unittest.TestCase):    def setUp(self):        self.headers = {"Content-Type": "application/json",                   ‘Accept‘: "application/json, text/plain, */*",                   "passport": login.login()                   }    def test_create_groups(self):        test_data = Do_Excel(r‘../test_data/test_data.xlsx‘, ‘python‘).get_data()        url = test_data[0][‘url‘]        data = eval(test_data[0][‘data‘])        res = requests.post(url, json=data,headers=self.headers)        print(res.text)        self.assertEqual(200, res.status_code)    def test_create_form(self):        test_data = Do_Excel(r‘../test_data/test_data.xlsx‘, ‘python‘).get_data()        url = test_data[1][‘url‘]        data = eval(test_data[1][‘data‘])        res = requests.post(url, json=data, headers=self.headers)        print(res.text)        self.assertEqual(200, res.status_code)执行测试用例,do_cases下的run_cases,代码:
import unittestimport HTMLTestRunnerNewfrom gongdan.test_cases import test_formcenter#创建容器suite = unittest.TestSuite()#创建加载器loader = unittest.TestLoader()#加载用例suite.addTests(loader.loadTestsFromModule(test_formcenter))with open(r‘D:/todo/gongdan/test_rusult/form.html‘,‘wb‘) as f :    runner = HTMLTestRunnerNew.HTMLTestRunner(stream=f,verbosity=2,title="表单中心")    runner.run(suite)测试报告展示:

接口自动化1.0 python+unittest

 接口自动化1.0 python+unittest