createTest.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. """
  4. 读yaml文件
  5. Author: songjian at <songjian12@baidu.com>
  6. Created: 2020/11/05
  7. """
  8. import sys
  9. from ruamel import yaml
  10. import logging
  11. import os
  12. import commands
  13. work_path = os.path.dirname(os.path.abspath(__file__))
  14. sdk_path = os.path.dirname(os.path.dirname(work_path))
  15. reload(sys)
  16. sys.setdefaultencoding('utf-8')
  17. def mkdir(version):
  18. """
  19. 初始化文件夹
  20. :param version:
  21. :return:
  22. """
  23. case_path = os.path.join(sdk_path, 'test_data/standard')
  24. version_path = os.path.join(sdk_path, 'test_data', version)
  25. cmd = 'mkdir -p %s' % version_path
  26. logging.info(commands.getoutput(cmd))
  27. cmd = 'rm -rf %s && cp -r %s %s' % (version_path + '/*', case_path + '/*', version_path)
  28. logging.info(commands.getoutput(cmd))
  29. test_path = os.path.join(sdk_path, 'test_cases', version)
  30. cmd = 'mkdir -p %s && rm -rf %s' % (test_path, test_path + '/*')
  31. logging.info(commands.getoutput(cmd))
  32. def write_file(yamlpath, num, filename, content, version):
  33. """
  34. 写测试文件
  35. :param version:
  36. :return:
  37. """
  38. f_path = 'test_' + '%02d' % num + '_' + filename + '.py'
  39. with open(os.path.join(sdk_path, 'test_cases', version, f_path), 'w') as f:
  40. for line in content:
  41. if line.find('PATH = ') == 0:
  42. line = 'PATH = sdk_path + "/test_data/%s/%s"\n' % (version, yamlpath)
  43. elif line.find('class Test') == 0:
  44. line = 'class Test%s:\n' % filename
  45. elif line.find(' def test_') == 0:
  46. line = ' def test_%s(self, case_data):\n' % filename
  47. elif line.find(' @pytest.mark.v') == 0:
  48. line = ' @pytest.mark.%s\n' % version
  49. f.write(line)
  50. def read_file(yaml_path, num, version):
  51. """
  52. 读模版文件
  53. :param filepath:
  54. :return:
  55. """
  56. with open(os.path.join(work_path, 'template.py')) as f:
  57. lines = f.readlines()
  58. filename = os.path.splitext(os.path.split(yaml_path)[1])[0]
  59. write_file(yaml_path, num, filename, lines, version)
  60. def create(version):
  61. """
  62. 新增版本
  63. :param version:
  64. :return:
  65. """
  66. mkdir(version)
  67. with open(sdk_path + '/conf/version.yaml', 'r') as fc:
  68. conf = yaml.load(fc.read(), Loader=yaml.RoundTripLoader)
  69. if version not in conf.keys():
  70. logging.error('%s version not exist' % version)
  71. sys.exit(-1)
  72. conf_list = conf[version]
  73. for i in range(len(conf_list)):
  74. yaml_path = conf_list[i].keys()[0]
  75. yaml_case = conf_list[i].values()[0]
  76. with open(sdk_path + '/test_data/' + version + '/' + yaml_path, 'r') as fc:
  77. yaml_data = yaml.load(fc.read(), Loader=yaml.RoundTripLoader)
  78. data_list = yaml_data['test_case']
  79. data = {}
  80. data['test_info'] = yaml_data['test_info']
  81. data['test_case'] = []
  82. for conf_item in yaml_case:
  83. flag = 0
  84. for data_item in data_list:
  85. if conf_item == data_item['test_name']:
  86. data['test_case'].append(data_item)
  87. flag = 1
  88. break
  89. if not flag:
  90. logging.error('%s not in %s' % (conf_item, yaml_path))
  91. sys.exit(-1)
  92. with open(sdk_path + '/test_data/' + version + '/' + yaml_path, 'w') as fw:
  93. yaml.dump(data, fw, Dumper=yaml.RoundTripDumper, allow_unicode=True)
  94. # case_path = os.path.join(sdk_path, 'test_data', version, yaml_path)
  95. read_file(yaml_path, i, version)
  96. if __name__ == "__main__":
  97. version = sys.argv[1]
  98. create(version)