Python uses template to replace variables in yaml file

background and purpose

Purpose: In the Android ui automation test, different data configuration files are automatically generated according to the different mobile devices connected each time

When doing Android UI automation testing, yaml files are usually used as test data or as configuration files. It is no problem to store static data in yaml files, and python data types are basically supported.
However, when multiple devices are in parallel, a specified set of test data needs to be allocated according to the uuid of the device, and the uuid of the device is read and assigned as a variable.

template use

template is a string template, used to replace variables in the string, it is a class reference variable of string, there are 2 formats

  •  The first $variable

from string import Template

tempTem = Template("My name is $name , i like $fancy")
d = {'name': 'admin', 'fancy': 'python'}

print(tempTem.substitute(d))

  • The second ${variable}
from string import Template

tempTemp1 = Template("My name is ${name} , i like ${fancy}")
d = {'name': 'admin', 'fancy': 'python'}
print(tempTemp1.substitute(d))

Assign values ​​to variables in the yaml template file and write another yaml

train of thought

  •  The data folder serves as a data-driven layer for automated testing

  •  checkin_data_template.yaml is the test template
udids:
    udid0: $udid0
    udid1: $udid1

mobile_login_data:
    $udid0:
        # 普通用户
        mobile_num: 13007135437
        login_psw: 123456

    $udid1:
        # 普通用户
        mobile_num: 13007135437
        login_psw: 123456

    
  •  checkin_data.yaml is to generate different data configuration files according to the different mobile phones connected each time
  • The effect after implementation is as follows, so that you don’t need to change the yaml file every time!
udids:
  udid0: 192.168.38.32:60176
  udid1: 192.168.38.32:60723
mobile_login_data:
  192.168.38.32:60176:
    mobile_num: 13007135437
    login_psw: 123456
  192.168.38.32:60723:
    mobile_num: 13007135437
    login_psw: 123456

 Implementation

        tmp_read = f.read()
        tmp_read_template = Template(tmp_read)
        tmp_target_yaml = tmp_read_template.safe_substitute(target_dict)
        tmp_target_python = yaml.safe_load(tmp_target_yaml)

  • First read the yaml template file checkin_data_template.yaml, then use safe_substitute to assign values ​​to variables, and finally convert the result to python format

        yaml.dump(tmp_target_python, f, allow_unicode=True, sort_keys=False)

  • Then convert the file in python format into an unsorted yaml file
# 根据yaml模板生成本次自动化yaml模板
def create_target_yaml(target_dict):
    '''
    target_dict:{"udid0": 1111, "udid1": 2222}
    '''
    if os.path.dirname(os.path.dirname(__file__)) == config_data.ROOT_DIR:
        yaml_data_path = os.path.dirname(os.path.dirname(__file__)) + '/data' + '/checkin_data_template.yaml'
    else:
        yaml_data_path = os.path.dirname(__file__) + '/data' + '/checkin_data_template.yaml'
    with open(yaml_data_path, encoding="utf-8") as f:
        tmp_read = f.read()
        tmp_read_template = Template(tmp_read)
        tmp_target_yaml = tmp_read_template.safe_substitute(target_dict)
        tmp_target_python = yaml.safe_load(tmp_target_yaml)
    if os.path.dirname(os.path.dirname(__file__)) == config_data.ROOT_DIR:
        yaml_data_path = os.path.dirname(os.path.dirname(__file__)) + '/data' + '/checkin_data.yaml'
    else:
        yaml_data_path = os.path.dirname(__file__) + '/data' + '/checkin_data.yaml'
    with open(yaml_data_path, 'w', encoding="utf-8") as f:
        # 不排序
        yaml.dump(tmp_target_python, f, allow_unicode=True, sort_keys=False)

Guess you like

Origin blog.csdn.net/qq_38312411/article/details/127284727