自动化测试用例如何进行参数化
参数化如官网所述,我们可以利用@ParameterizedTest+@ValueSource或@CsvSource进行参数化设置。
http://www.51testing.com/attachments/2021/04/15326880_202104271503451WmDo.jpghttp://www.51testing.com/attachments/2021/04/15326880_202104271504041g4kg.pnghttp://www.51testing.com/attachments/2021/04/15326880_202104271504131IaIt.png
参数化实例:
以雪球APP股票搜索功能为例
@ParameterizedTest
@CsvSource({
"滴滴,滴滴出行",
"alibaba,阿里巴巴",
"sougou,搜狗"
})
public void 搜索股票(String searchInfo,String exceptName) {
String name = searchpage.inputSearchInfo(searchInfo).getAll().get(0);
assertThat(name,equalTo(exceptName));
}
数据文件驱动
官网中给出了@CsvFileSource的方法:
http://www.51testing.com/attachments/2021/04/15326880_202104271504221fQY7.png
csv数据文件驱动实例:
csv数据文件:
pdd xiaomi
pdd
测试用例demo
@ParameterizedTest
@CsvFileSource(resources = "/data/SearchTest.csv")
void 选择(String keyword){
ArrayList<String> arrayList = searchpage.inputSearchInfo("xiaomi").addSelected();
}
利用yaml文件进行数据文件驱动
先看如何对yaml文件进行数据操作
官方地址:https://github.com/FasterXML/jackson-dataformats-text/tree/master/yaml
阮一峰教程:http://www.ruanyifeng.com/blog/2016/07/yaml.html
从官网中可以得到如下信息
Maven dependency
To use this extension on Maven-based projects, use following dependency:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.9.2</version>
</dependency>
Usage
Simple usage
Usage is as with basic JsonFactory; most commonly you will just construct a standard ObjectMapper with com.fasterxml.jackson.dataformat.yaml.YAMLFactory, like so:
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
User user = mapper.readValue(yamlSource, User.class);
利用官网提供的信息可以封装方法对yaml文件进行操作:
public static GlobalConfig load(String path){
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
try {
GlobalConfig config = mapper.readValue(GlobalConfig.class.getResource(path), GlobalConfig.class);
return config;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
再来看Junit5提供的一个方法 @MethodSource
http://www.51testing.com/attachments/2021/04/15326880_2021042715043217Xk8.png
利用此方法加载yaml文件中的数据,进行参数传递,完成数据文件驱动的目的
· yaml文件:
xqDemoConfig:
username: 888
password: 666
testdata:
滴滴: 滴滴出行
alibaba: 阿里巴巴
sougou: 搜狗
测试用例demo
@ParameterizedTest
@MethodSource("YamlData")
public void 搜索股票2(String searchInfo,String exceptName) {
String name = searchpage.inputSearchInfo(searchInfo).getAll().get(0);
assertThat(name,equalTo(exceptName));
}
static Stream<Arguments> YamlData(){
GlobalConfig config =GlobalConfig.load("/data/globalConfig.yaml");
List<Arguments> list = new ArrayList<>();
Arguments arguments = null;
for (String key : config.xqDemoConfig.testdata.keySet()) {
Object value = config.xqDemoConfig.testdata.get(key);
arguments = arguments(key, value);
list.add(arguments);
}
return Stream.of(list.get(0),list.get(1),list.get(2));
}
效果演示:
http://www.51testing.com/attachments/2021/04/15326880_202104271504411UNXY.gif
学习下
页:
[1]