junit5数据驱动

junit5官方文档:JUnit 5 用户指南

一、使用yaml文件实现junit5数据驱动:

1.在IDEA中新建一个工程,src的resources目录下新建一个yaml文件,如图所示:

2.在yaml文件中配置需要的参数,例如我们测试登录接口,测试不同的电话号码和密码是否可以登录,只需要在yaml文件中配置不同的电话号码和密码即可:

3.在测试用例方法前增加以下注解:

    @ParameterizedTest
    @CsvFileSource(resources ="/loginParameter.yaml")
    public void loginTest2(String mobile,String password){
       Response loginResponse = LoginTest.loginByiMobile(mobile,password);
       Integer stateCode = loginResponse.path("stateCode");
       String access_token=loginResponse.path("data.access_token");

      // assertTrue (stateCode.equals(200));
        assertAll("stateCode",
                () -> assertEquals("200", stateCode.toString()),
                () -> assertNotNull(access_token)
        );

    }

二、在注解中直接传参

    @ParameterizedTest
    @CsvSource({
            "13531764015, 123456",
            "18175465437, 123456",
            "18475465438, 123456"
    })
    public void loginTest1(String mobile,String password){
       Response loginResponse = LoginTest.loginByiMobile(mobile,password);
       Integer stateCode = loginResponse.path("stateCode");
       String access_token=loginResponse.path("data.access_token");

       assertTrue (stateCode.equals(200));
        System.out.println(access_token);
    }

在传入多个参数后,如何对返回的多个结果进行断言呢?可以把需要断言的字段,也写在传参里面,例如要对返回的stateCode进行断言,代码如下:

    @ParameterizedTest
    @CsvSource({
            "13536764011, 123456,200",
            "18375465437, 123456,200",
            "18575465438, 123456,500"
    })
    public void testLogin(String mobile,String password,String expectStateCode){
       Response loginResponse = LoginTest.loginByiMobile(mobile,password);
       Integer stateCode = loginResponse.path("stateCode");
       String access_token=loginResponse.path("data.access_token");

       assertTrue(stateCode.equals(Integer.valueOf(expectStateCode)));

        System.out.println(access_token);
        System.out.println(stateCode);
    }

  

posted @ 2021-06-16 11:24  zhanchenglan  阅读(179)  评论(0编辑  收藏  举报