通用技术 junit5-TestExecutionListener,参数化测试的时候失败的 test,获取结果 “SUCCESSFUL”

shixing · 2020年04月07日 · 最后由 shixing 回复于 2020年04月08日 · 2256 次阅读

java test code


import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;


public class test {
    @ParameterizedTest
    @MethodSource("userGenerator")
    void testFail1(String name,String pw) {
        Assertions.assertEquals(name,pw);
    }
    public static Stream<Arguments> userGenerator(){
        return Stream.of(Arguments.of("15138493452", "hlf2016"));
    }
}
public class TestListener implements TestExecutionListener {
  /*
    *   ... ...
    */

public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {


}
   if ("SUCCESSFUL".equals(testExecutionResult.getStatus().name())) {

         // testExecutionResult.getStatus().name() = "SUCCESSFUL"
  }

}

调试截图

截图

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 5 条回复 时间 点赞

junit5 没用过,这个如果你跳转 testExecutionResult.getStatus().name() 看看源码是怎么样得。

陈子昂 回复

问题提交到开源,收到回复,如下:

@ParameterizedTest method is technically a "container" in JUnit 5, but the individual parameterized test method invocations below that container are "tests".

认为@ParameterizedTest是一个容器,而非一个 test 。

赞,你的寻求帮助的方式很正能量。那再加一个 test 的注释?

恒温 回复

@Test @ParameterizedTest两个注解不能同时用, @ParameterizedTest 认为是一个容器,容器中存放 test。
共同使用会有冲突,或者被重复执行。

单独使用@ParameterizedTest 时,这个容器下会使用传递的参数自动创建 tests(个人理解),所以 是我的判读方法有问题,不能直接去用 testExecutionResult.getStatus().name() 去判断,因为可能拿到的是容器的执行结果 。

正确的判断是否为 testIdentifier.isTest(),会返回一个布尔值 ,确定是用例时,在去通过 testExecutionResult.getStatus().name() 取用例执行结果

大概就这是这样子

贴上 issue 地址,https://github.com/junit-team/junit5/issues/2247
感谢大神的答疑解惑。

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册