• 自动化测试用例如何进行参数化


    利用CSV、yaml文件等进行数据文件驱动(基于Junit5 @CSVSource、@MethodSource等)

    Junit5官方说明地址:https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests
    中文版:https://sjyuan.club/junit5/user-guide-cn/

    参数化
    如官网所述,我们可以利用@ParameterizedTest+@ValueSource或@CsvSource进行参数化设置

    参数化实例:
    以雪球APP股票搜索功能为例
    @ParameterizedTest
    @CsvSource({
    "滴滴,滴滴出行",
    "alibaba,阿里巴巴",
    "sougou,搜狗"
    })
    public void 搜索股票(String searchInfo,String exceptName) {
    String name = searchpage.inputSearchInfo(searchInfo).getAll().get(0);
    assertThat(name,equalTo(exceptName));
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    数据文件驱动
    官网中给出了@CsvFileSource的方法

    csv数据文件驱动实例:
    csv数据文件:
    pdd
    xiaomi
    pdd
    1
    2
    3
    测试用例demo

    @ParameterizedTest
    @CsvFileSource(resources = "/data/SearchTest.csv")
    void 选择(String keyword){
    ArrayList<String> arrayList = searchpage.inputSearchInfo("xiaomi").addSelected();
    }
    1
    2
    3
    4
    5
    利用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>
    1
    2
    3
    4
    5
    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);
    1
    2
    利用官网提供的信息可以封装方法对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;
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    再来看Junit5提供的一个方法 @MethodSource


    利用此方法加载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));

    原文链接:https://blog.csdn.net/weixin_43291944/article/details/95061580

  • 相关阅读:
    linux下安装qt-4.5_for_TQ210_V1.0.(TQ210)-ubuntu11.10过程出现的问题
    15+ 易响应的CSS框架快速开启你的敏捷网站项目
    分享10个超实用的jQuery代码片段
    免费数据库+免费空间建站
    25个令人难忘的广告设计
    android实现点击效果
    ServletContextListener
    ServletContext
    一个不错的地址--类序列化等
    Java的运行时数据存储机制
  • 原文地址:https://www.cnblogs.com/zgq123456/p/12842474.html
Copyright © 2020-2023  润新知