junit5 yaml和json 数据驱动
使用yaml 数据驱动测试
新建用例数据类,用于动态修改测试数据
对应yaml文件,第一层结构为列表 第二层结构为具体序列化class对象
测试成功
public class TestYaml {
@ParameterizedTest()
@MethodSource
public void testDDTFromYaml(User user) {
assertTrue(user.name.length() > 3);
}
static List<User> testDDTFromYaml() throws IOException {
//创建objmapper
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
TypeReference typeReference = new TypeReference<List<User>>() {
};
//读取useryaml
List<User> users = mapper.readValue(
TestYaml.class.getResourceAsStream("/user.yaml"),typeReference
);
return users;
}
}
使用json 数据驱动测试
动态修改测试用例
@ParameterizedTest
@MethodSource
public void testDDTFromJson(User user) {
assertTrue(user.name.length() > 3);
}
static List<User> testDDTFromJson() throws IOException {
//创建objmapper
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
TypeReference typeReference = new TypeReference<List<User>>() {
};
//读取useryaml
List<User> users = mapper.readValue(
TestYaml.class.getResourceAsStream("/user.json"),typeReference
);
return users;
}
}