1、新建一个注解,方便查看方法读取的yaml的具体内容
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestCaseFlorder {
String casePath() default "";
}
2、在resource目录下新建一个yaml文件,文件中输入数据
caseName: case001
description: desc001
dataItem:
a: 34
b: 58
---
#多个用例数据用"---"分开
caseName: case002
description: desc002
dataItem:
a: 23
b: 89
3、测试方法
@Test
@TestCaseFlorder(casePath = "/testPath/data.yaml" )
public void getYamldata() throws Exception {
String path = "src/main/resources";
TestCaseFlorder testCaseFlorder = YamlFileTest.class.getMethod("getYamldata").getAnnotation(TestCaseFlorder.class);
File file = new File(path+testCaseFlorder.casePath());
FileInputStream fileInputStream = new FileInputStream(file);
Yaml yaml = new Yaml();
Iterable<Object> objects = yaml.loadAll(fileInputStream);
for (Object yamldata: objects) {
System.out.println(yamldata);
}
}
}
结果: