Continuing test execution in junit4 even when one of the asserts fails

后端 未结 6 1242
无人及你
无人及你 2020-12-02 16:55

I have my existing framework built up using Jfunc which provides a facility to continue exection even when one of the asserts in the test case fails. Jfunc uses junit 3.x f

6条回答
  •  庸人自扰
    2020-12-02 17:07

    Try - catch, in "try" use the assertion, in "catch" add the possible error to collection. Then throw the exception at the end of test, in tearDown(). So if there will be fail/error in assert, it will be catched and test will continue. (The collection in example is static, you can also make new instance in setUp() for each @Test)

        public static List errors = new ArrayList<>();
    
    
        try {
            //some assert...
        }
        catch (AssertionError error) {
            errors.add(error.toString());
        }
    
    
    
        @After
        public void tearDown() {
    
            try {
                if (!errors.isEmpty()) {
                    throw new AssertionError(errors);
                }
            }
            finally {
                //empty list because it's static, alternatively make instance for each test in setUp()
                errors.clear();
            }
        }
    

提交回复
热议问题