123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #!/usr/bin/env python
- # -*- coding:utf-8 -*-
- """
- 读yaml文件
- Author: songjian at <songjian12@baidu.com>
- Created: 2020/11/05
- """
- import sys
- from ruamel import yaml
- import logging
- import os
- import commands
- work_path = os.path.dirname(os.path.abspath(__file__))
- sdk_path = os.path.dirname(os.path.dirname(work_path))
- reload(sys)
- sys.setdefaultencoding('utf-8')
- def mkdir(version):
- """
- 初始化文件夹
- :param version:
- :return:
- """
- case_path = os.path.join(sdk_path, 'test_data/standard')
- version_path = os.path.join(sdk_path, 'test_data', version)
- cmd = 'mkdir -p %s' % version_path
- logging.info(commands.getoutput(cmd))
- cmd = 'rm -rf %s && cp -r %s %s' % (version_path + '/*', case_path + '/*', version_path)
- logging.info(commands.getoutput(cmd))
- test_path = os.path.join(sdk_path, 'test_cases', version)
- cmd = 'mkdir -p %s && rm -rf %s' % (test_path, test_path + '/*')
- logging.info(commands.getoutput(cmd))
- def write_file(yamlpath, num, filename, content, version):
- """
- 写测试文件
- :param version:
- :return:
- """
- f_path = 'test_' + '%02d' % num + '_' + filename + '.py'
- with open(os.path.join(sdk_path, 'test_cases', version, f_path), 'w') as f:
- for line in content:
- if line.find('PATH = ') == 0:
- line = 'PATH = sdk_path + "/test_data/%s/%s"\n' % (version, yamlpath)
- elif line.find('class Test') == 0:
- line = 'class Test%s:\n' % filename
- elif line.find(' def test_') == 0:
- line = ' def test_%s(self, case_data):\n' % filename
- elif line.find(' @pytest.mark.v') == 0:
- line = ' @pytest.mark.%s\n' % version
- f.write(line)
- def read_file(yaml_path, num, version):
- """
- 读模版文件
- :param filepath:
- :return:
- """
- with open(os.path.join(work_path, 'template.py')) as f:
- lines = f.readlines()
- filename = os.path.splitext(os.path.split(yaml_path)[1])[0]
- write_file(yaml_path, num, filename, lines, version)
- def create(version):
- """
- 新增版本
- :param version:
- :return:
- """
- mkdir(version)
- with open(sdk_path + '/conf/version.yaml', 'r') as fc:
- conf = yaml.load(fc.read(), Loader=yaml.RoundTripLoader)
- if version not in conf.keys():
- logging.error('%s version not exist' % version)
- sys.exit(-1)
- conf_list = conf[version]
- for i in range(len(conf_list)):
- yaml_path = conf_list[i].keys()[0]
- yaml_case = conf_list[i].values()[0]
- with open(sdk_path + '/test_data/' + version + '/' + yaml_path, 'r') as fc:
- yaml_data = yaml.load(fc.read(), Loader=yaml.RoundTripLoader)
- data_list = yaml_data['test_case']
- data = {}
- data['test_info'] = yaml_data['test_info']
- data['test_case'] = []
- for conf_item in yaml_case:
- flag = 0
- for data_item in data_list:
- if conf_item == data_item['test_name']:
- data['test_case'].append(data_item)
- flag = 1
- break
- if not flag:
- logging.error('%s not in %s' % (conf_item, yaml_path))
- sys.exit(-1)
- with open(sdk_path + '/test_data/' + version + '/' + yaml_path, 'w') as fw:
- yaml.dump(data, fw, Dumper=yaml.RoundTripDumper, allow_unicode=True)
- # case_path = os.path.join(sdk_path, 'test_data', version, yaml_path)
- read_file(yaml_path, i, version)
- if __name__ == "__main__":
- version = sys.argv[1]
- create(version)
|