背景: 项目中有多个测试用例,测试用例使用yaml的配置文件,各个测试用例的yaml配置文件中有一些共有的属性,

把这些共有的属性抽取出来放在一个yaml文件中,问题来了:如何在测试用例的Yaml文件引用这些共享的Yaml文件:

 

测试用例的yaml文件:document.yml

languages:
- Ruby
- Perl
- Python
websites:
YAML: yaml.org
Ruby: ruby-lang.org
Python: python.org
Perl: use.perl.org

test: !include
doc01.yml


共享的yaml文件: doc01.yml

demo01:
- Ruby

 

python实现:

import yaml
import os


def yaml_include(loader, node):
# Get the path out of the yaml file
file_name = os.path.join(os.path.dirname(loader.name), node.value)

with file(file_name) as inputfile:
return yaml.load(inputfile)

yaml.add_constructor("!include", yaml_include)
stream = file('document.yml', 'r')
print yaml.dump(yaml.load(stream))






打印结果:

languages: [Ruby, Perl, Python]
test:
  demo01: [Ruby]
websites: {Perl: use.perl.org, Python: python.org, Ruby: ruby-lang.org, YAML: yaml.org}

posted on 2018-01-09 21:28  robynn  阅读(3889)  评论(0编辑  收藏  举报