本系列汇总,请查看这里:https://www.cnblogs.com/uncleyong/p/18033074
实现目标
将initSql中常用变量占位符(下图划线的)替换为实际值。
添加替换变量工具模块
substitution_tool.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author: 韧 # @wx: ren168632201 # @Blog: https://www.cnblogs.com/uncleyong/ import logging logger = logging.getLogger(__name__) from utils.common_var_tool import cv def var_substitution(param): '''常用变量占位符替换为实际值''' var_dic = cv.getCommonVar() logger.info("常用变量:{}".format(var_dic)) for key in var_dic: if key in param: param = param.replace(key, str(var_dic[key])) # replace返回替换后的新字符串 return param
修改测试类:获取initSql并替换
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author: 韧 # @wx: ren168632201 # @Blog: https://www.cnblogs.com/uncleyong/ import pytest import logging logger = logging.getLogger(__name__) from conf.settings import CASE_DATA_PATH from utils.yaml_tool import read_data_from_yaml from utils.substitution_tool import var_substitution class TestCase: @pytest.mark.parametrize("casedata", read_data_from_yaml(CASE_DATA_PATH)) def test_case(self, casedata): # logger.info(f"---当前测试用例数据是:{casedata}") # 获取initSql initSql = casedata["initSql"] # 类型是list # logger.info(type(initSql)) # logger.info(">>>>>initSql:{}".format(initSql)) if initSql: for sql in initSql: logger.info("==================替换前sql:{}".format(sql)) sql = var_substitution(sql) logger.info(f"==================替换后sql:{sql}")
运行结果
可以看到占位符都成功替换为了对应常用变量值
标签:info,06,python,initSql,sql,var,import,logger From: https://www.cnblogs.com/uncleyong/p/18047612