一、背景
之前一直都在学习接口自动化,工作中测试接口的案例很多,但是真正能做自动化的很少,一个接口手工测完验证通过就完成上线了,直到近一年接触实时告警,让我在工作中能实践接口自动化了。一个看似很简单的接口,但是接口背后的处理逻辑比较复杂,接口的逻辑这里不细说,今天主要看看我是怎么实现接口自动化的。
二、代码示例
代码目录结构,主要利用python+pytest+allure框架实现。
1、case下主要存放测试用例,用例截图
2、common目录存放公共代码和日志代码
1 1 import requests,json,random 2 2 from Testcase.common.logger import logger 3 3 4 4 class Cardcenter(): 5 5 def api_checkwarning(self,words, role): 6 6 url = "http://IP:PORT/api/realtime/checkwarning" 7 7 headers = { 8 8 'Content-Type': 'application/json' 9 9 } 10 10 data = { 11 11 "role": "2", 12 12 "words": "85731928", 13 13 "ucid": "ea02er1001", 14 14 "agent_id": "85731928", 15 15 "extension_num": "10086" 16 16 } 17 17 data["ucid"] = str(random.randint(10000000,99999999)) 18 18 data["words"] = str(words) 19 19 data["role"] = str(role) 20 20 # print(data) 21 21 logger.debug(f'请求参数:{data}') 22 22 response = requests.post(url=url, headers=headers, data=json.dumps(data)) 23 23 #logger.debug(f'{response}') 24 24 result = response.json() 25 25 return result 26 26 27 27 def go_warning(self,words, id, role): 28 28 num_1 = 0 29 29 result = self.api_checkwarning(words, role) 30 30 list_1 = result["data"]["CheckResults"] 31 31 logger.debug(f'返回结果: {list_1}') 32 32 print(list_1) 33 33 if list_1 == []: 34 34 result_01 = '未命中告警' 35 35 elif list_1 is None: 36 36 result_01 = '未命中告警' 37 37 else: 38 38 for j in range(len(list_1)): 39 39 if list_1[j]['ruleId'] == id: 40 40 num_1 += 1 41 41 else: 42 42 pass 43 43 if num_1 == 1: 44 44 result_01 = '命中告警' 45 45 elif num_1 >= 2: 46 46 result_01 = '命中告警' 47 47 else: 48 48 result_01 = '未命中告警' 49 49 return result_01
1 import logging 2 3 logger = logging.getLogger("cnodeapp") 4 logger.setLevel(logging.DEBUG) 5 6 7 format = logging.Formatter('[%(asctime)s] [%(levelname)s] [%(funcName)s] %(message)s') 8 9 fl = logging.FileHandler(filename='Testcase/logs/cnode.log',mode='a',encoding='utf8') 10 fl.setFormatter(format) 11 12 sl = logging.StreamHandler() 13 sl.setFormatter(format) 14 15 logger.addHandler(fl) 16 logger.addHandler(sl)
3、lib是安装allure目录
4、logs目录存放日志
5、report目录生成测试报告
6、testcase执行代码文件
import pytest import pandas as pd from Testcase.common.common import Cardcenter def pd_excel(): df01 = pd.read_excel('D:/untitled/Testcase/case/Go_warning.xlsx', sheet_name='checkwarning') print(df01) role_01 = df01['角色'] role_01 = list(role_01) print(role_01) id_01 = df01['规则ID'] id_01 = list(id_01) words_01 = df01['话术'] words_01 = list(words_01) print(words_01) result_02 = df01['预期结果'] result_02 = list(result_02) data = [] for i in range(len(words_01)): b = (words_01[i],id_01[i],role_01[i],result_02[i]) data.append(b) return data @pytest.mark.parametrize('words,id,role,result_02', pd_excel()) class Testwarn(): def test_warning(self,words, id, role,result_02): assert Cardcenter().go_warning(words, id, role) == result_02
三、生成测试报告命令
1、生成json测试报告命令:pytest Testcase estcase --alluredir Testcase
eport/allure_raw
2、生成html报告,衔接进入liballure-2.13.8in目录下,执行命令:allure generate D:untitledTestcase eportallure_raw -o D:untitledTestcase eportallure_report --clean
3、生成测试报告后在浏览器中打开测试报告