简体   繁体   中英

JUnit just log failures?

how can I use JUnit to not terminate on assertion failures, but just log them?

My goal is to fail if log messages exist, and print them out. Because I'd like to iterate over a big list, and want to know which elements fail.

How can I force JUnit to not break on failures?

ty

也许ErrorCollector规则会帮助您

如果要查看测试可能失败的所有原因,而不仅仅是第一个原因,则需要创建测试失败的原因列表,如果列表结尾不为空,则抛出错误。

You can't do what you want with standard JUnit, with your tests as they are. The problem is that assertXXX methods actually throw Exceptions (AssertionError), so you can't use normal asserts and resume from the point after the exception has been thrown. JUnit catches these AssertIonErrors and does the right thing.

One alternative is to use, as Ludwig suggested, the ErrorCollector rule, but it will mean rewriting a fair portion of your tests I would imagine.

If your goal is to iterate over a large list, look at Parameterized . This allows you to iterate over single test method, with different data each time:

@RunWith(Parameterized.class)
public class FibonacciTest {
  @Parameters
  public static List<Object[]> data() {
    return Arrays.asList(new Object[][] {
      { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
    });
  }

  private int fInput;

  private int fExpected;

  public FibonacciTest(int input, int expected) {
    fInput= input;
    fExpected= expected;
  }

  @Test
  public void test() {
    assertEquals(fExpected, Fibonacci.compute(fInput));
  }
}

data() returns a list of Object[]. Each Object[] in the list is passed to the constructor of the test class. So a new instance of the test class is used for each entry in the list.

The number of entries in the Object[] must correspond to the number of parameters to the constructor, and the types have to correspond as well.

如果您在junit任务中使用诸如ant之类的构建工具,则可以将no设置为haltonfailure属性,以便在测试失败时不会停止构建过程。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM