如何运行所有测试用例,连前面的测试用例都错了

How to run all test cases ,Even the previous test cases are wrong

我刚开始尝试 JUnit。我创建了一些测试用例。但是,当我发现任何错误的测试用例时,测试用例将停止。我想 运行 通过每个测试用例,即使有很多错误的测试用例。

例如

assertEquals ( "this test case will be shown", Main.plus ( 1,2 ),3 );
assertEquals ( "this first wrong test case will be shown", Main.plus ( 1, 2 ), 4 );
assertEquals ( "this first wrong test case **won't be shown**", Main.plus ( 1, 2 ), 4 );

我想让第三个这个case是运行(证明是错的)

注意: ErrorCollector 规则允许在发现第一个问题后继续执行测试(例如,收集 table 中所有不正确的行并立即报告它们):

这里有更多信息

http://junit.org/apidocs/org/junit/rules/ErrorCollector.html

断言不是测试用例。失败的断言将抛出一个异常,如果未被捕获,该异常将向上传播并且不会执行其余测试。

您的解决方案是将这些断言中的每一个放入不同的测试中。

还有一个旁注,通常断言的第一个参数是预期值,所以我会交换输入。

@Test
public void correctAddition(){
        assertEquals(3, Main.plus(1,2));
}

@Test
public void wrongAddition(){
        //test will fail
        assertEquals(4, Main.plus(1,2));
}

@Test
public void wrongAddition2(){
        //test will also fail
        assertEquals(4, Main.plus(1,2));
}