Don't let test stop on failure

2019-01-15 20:48发布

I'm looking for the best practice for following (simplified) scenario:

@Test
public void someTest() {
  for(String someText : someTexts) {
    Assert.true(checkForValidity(someText));
  }
}

This test iterates through x-thousands of texts and in this case I don't want it to be stopped for each failure. I want the errors to be buffered and in case of error(s) to fail at the end. Has JUnit got something on board for for my aim?

标签: junit junit4
1条回答
欢心
2楼-- · 2019-01-15 21:40

First of all, it's not really the correct way to implement this. JUnit allows parametrizing tests by defining a collection of inputs/outputs with the Parametrized test runner. Doing it this way ensures that each test case becomes a unique instance, making test report clearly state which samples passed and which ones failed.

If you still insist on doing it your way you should have a look at AssertJ's Soft Assertions which allow "swallowing" individual assertion failures, accumulating them and only reporting after the test is finished. The linked documentation section uses a nice example and is definitely worth reading.

查看更多
登录 后发表回答