本文介绍了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);
   }
}

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

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

public class SnippetTest {

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

Theories Documentation 矛盾.

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

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假设失败了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 11:29