目标:
一:完成手机号码的自加,new_do_excl.py
二:集成单元测试,ddt,http请求、写回到excl,并且生成测试报告
三、增加配置文件,实现测试用例可配置
new_do_excl.py (手机号码自加)
from openpyxl import load_workbook class DoExcl: def __init__(self, file_name): self.file_name = file_name def get_data(self, button, case_id_list): file = load_workbook(self.file_name) sheet = file["testcase"] test_data = [] new_tel = file["tel"].cell(1, 1).value # 整数、浮点数、从Excle里面去出是整数、浮点数,字符串,列表,字典,从excl里面取出是字符串 for i in range(2, sheet.max_row+1): # +1取左不取右,max_clumn sub_data = {} sub_data["id"] = sheet.cell(i, 1).value sub_data["HttpMethod"] = sheet.cell(i, 2).value sub_data["module"] = sheet.cell(i, 3).value sub_data["description"] = sheet.cell(i, 4).value sub_data["url"] = sheet.cell(i, 5).value if sheet.cell(i, 6).value.find('${tel}') != -1: sub_data['param'] = sheet.cell(i, 6).value.replace('${tel}', str(new_tel)) else: sub_data["param"] = sheet.cell(i, 6).value sub_data["ExpectedResult"] = sheet.cell(i, 7).value test_data.append(sub_data) file["tel"].cell(1, 1).value = new_tel+1 file.save(self.file_name) if button == 'on': finally_data = test_data else: finally_data = [] for item in test_data: if item['id'] in eval(case_id_list): finally_data.append(item) # print(test_data) return finally_data def write_back(self, row, ActualResult, TestResult): wb = load_workbook(self.file_name) sheet = wb["testcase"] sheet.cell(row, 8).value = ActualResult sheet.cell(row, 9).value = TestResult wb.save(self.file_name) # if __name__ == '__main__': # test_data = DoExcl('api.xlsx').get_data() # print("测试数据是:{}".format(test_data)) test_api.py(集成单元测试,ddt装饰,读取配置文件实现用例执行可以配置,批量写回结果)
import unittest from ddt import ddt, data from class_2019_API_unnitest.new_do_excle import DoExcl from class_2019_API_unnitest.http_request import HttpMethod from class_2019_API_unnitest.read_config import ReadConfig button = ReadConfig().read_config("case.config", "CONFIG", "button") case_id_list = ReadConfig().read_config("case.config", "CONFIG", "case_id_list") test_data = DoExcl("api.xlsx").get_data(button, case_id_list)
# 读取配置控制执行哪条测试用例,实际上是对测试数据的读取进行配置
@ddt class TestApi(unittest.TestCase): def setUp(self): self.t = DoExcl("api.xlsx") print("开始测试") @data(*test_data) def test_api(self, data_item): print("正在执行第{0}条测试用例{1}".format(data_item['id'],data_item['description'])) print("测试数据是{0}".format(data_item['param'])) url = 'http://47.107.168.87:8080/futureloan/mvc/api/member/register' res = HttpMethod().http_request(url, eval(data_item['param']), data_item['HttpMethod']) # eval()函数将字符串转换成字典 try: self.assertEqual(str(data_item["ExpectedResult"]),res['code']) TestResult = 'PASS' except AssertionError as e: TestResult = 'failed' raise e finally: self.t.write_back(data_item["id"]+1, res['code'], TestResult) # 将结果批量写回到excl def tearDown(self): print("测试结束了") http_request.py import requests
class HttpMethod: def http_request(self,url,param,http_method): if http_method.upper() =='POST': try: res = requests.post(url, param) except Exception as e: print("post注册请求出错,错误是{}".format(e)) else: try: res = requests.get(url, param) except Exception as e: print("post注册请求出错,错误是{}".format(e)) print("http请求的结果是{}".format(res.json())) return res.json() # 增加返回值,在请求正常的情况下有返回值
read_config.py
from configparser import ConfigParser class ReadConfig: def read_config(self, filename, section, option): cf = ConfigParser() cf.read(filename) value = cf.get(section, option) return value if __name__ == '__main__': ReadConfig().read_config("case.config", "CONFIG", "button") case.config
[CONFIG] button=on case_id_list=[1,3] run.py(单元测试执行测试用例)
import unittest from class_2019_API_unnitest.common import test_api import HTMLTestRunnerNew suite = unittest.TestSuite()#测试容器 loader = unittest.TestLoader()# 执行测试用例 suite.addTest(loader.loadTestsFromModule(test_api)) #按照模块执行 with open ('test_result.html', "wb+") as file: # 测试报告 runner = HTMLTestRunnerNew.HTMLTestRunner(file, title='测试报告', description='软件测试', tester='林越') runner.run(suite)