【问题标题】:How to stop / kill my current test case?如何停止/杀死我当前的测试用例?
【发布时间】:2018-07-16 13:25:01
【问题描述】:

如果测试与特定场景匹配,我想停止执行测试,以避免代码重复。

考虑以下情况:

CoreProviderTest

  public void executeCoreSuccess(Object responseModel){
     assertNotNull("Response successful", responseModel != null);

     if (responseModel == null) {
        //Kill Test
     }
  }

ChildProviderTest - 扩展 CoreProviderTest

 @Test
public void responseTester() {

    new Provider().getServiceResponse(new Provider.Interface() {
        @Override
        public void onSuccess(Object responseModel) {
            executeCoreSuccess(responseModel);
            //Continue assertions
        }

        @Override
        public void onFailure(ErrorResponseModel error) {
            executeCoreFailure(error);
        }
    });
}

对于空响应,我想在 CoreProviderTest 中终止我当前的测试用例,否则可能会在进一步的断言中触发异常。我想避免这样的事情:

CoreProviderTest

if (responseModel == null) {
    return true;
}

ChildProviderTest

@Override
public void onSuccess(Object responseModel) {
   if (executeCoreSuccess(responseModel))
       return;

     //Continue assertions
}

有没有办法用 Mockito、JUnit 或 Roboletric 终止当前的测试执行?到目前为止没有运气搜索答案。

提前致谢

【问题讨论】:

标签: unit-testing junit automated-tests mockito robolectric


【解决方案1】:

如果您使用的是 JUnit5,它具有假设、禁用测试和条件测试执行等功能。 这是链接:

https://junit.org/junit5/docs/current/user-guide/#writing-tests-assumptions

在你的情况下,看起来像假设应该工作。这是 API: https://junit.org/junit5/docs/5.0.0/api/org/junit/jupiter/api/Assumptions.html#assumingThat-boolean-org.junit.jupiter.api.function.Executable-

【讨论】:

    【解决方案2】:

    JUnit Assumptions 非常适合给定的情况。

    现在的代码 sn-p 是这样的:

    CoreProvider

     public void executeCoreSuccess(Object responseModel){
        assumeTrue("Response successful",responseModel != null);
     }
    

    根据 JUnit 的文档:

    假设失败并不意味着代码被破坏,而是意味着 测试没有提供有用的信息。假设基本上意味着“不要运行 如果这些条件不适用,则进行此测试”。默认的 JUnit 运行器 跳过假设失败的测试。

    +1 阿德林和多萨尼

    【讨论】:

      猜你喜欢
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多