JUnit假设失败了 [英] JUnit assumptions fail Theories

查看:112
本文介绍了JUnit假设失败了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把JUnit @Theory弄得一团糟,发现assumeTrue(false)如果忽略它,就无法通过instrad理论.

I was messing arround with JUnit @Theory and find out that assumeTrue(false) fails the theory instrad if ignore it.

此代码未通过测试:

@RunWith(Theories.class)
public class SnippetTest {

   @Theory
   public void validateIndices(){
       assumeTrue(false);
   }
}

SnippetTest.validateIndices从未找到满足条件的参数 方法假设.违反的假设: [org.junit.AssumptionViolatedException:得到:错误,预期:是 是]

SnippetTest.validateIndices Never found parameters that satisfied method assumptions. Violated assumptions: [org.junit.AssumptionViolatedException: got: false, expected: is true]

但是当我使用@Test假设时,请忽略它.

But when I'm using @Test assumption ignore it.

public class SnippetTest {

    @Test
    public void validateIndices() {
        assumeTrue(false);
    }
}

Theories Documentation 矛盾.

如果任何假设失败,则数据点将被静默忽略.

If any of the assumptions fail, the data point is silently ignored.

我想念什么或做错了什么?

What am I missing or what am doing wrong?

推荐答案

感谢@TamasRev评论,我发现出了什么问题.看起来,如果所有假设都失败了,它将使测试失败.在我发布的情况下,我只有一个假设. 如果我使用@DataPoints会怎样? 这也失败了

Thanks to @TamasRev comment I found what was going wrong. Looks like, it will fail the test in case all the the assumption fails. Im the case I posted, I have only one assumption. What is happening if I'm using @DataPoints? This one fails as well

@RunWith(Theories.class)
public class SnippetTest {

    @DataPoints
    public static boolean[] data(){
        return new boolean[]{false, false};
    }

   @Theory
   public void validateIndices(boolean data){
       assumeTrue(data);
       assertTrue(true);
   }
}

但是,如果至少一项假设通过了,那么测试就不会失败.

But when if at least one assumption pass then test is not failed.

@RunWith(Theories.class)
public class SnippetTest {

    @DataPoints
    public static boolean[] data(){
        return new boolean[]{false, true};
    }

   @Theory
   public void validateIndices(boolean data){
       assumeTrue(data);
       assertTrue(true);
   }
}

感谢@TamasRev向我指出正确的方向.

Thanx @TamasRev for pointing me to right direction.

这篇关于JUnit假设失败了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆