如何运行所有测试用例,即使以前的测试用例也是错误的

UmA*_*orn 0 java junit android android-espresso

我刚刚开始试用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 );
Run Code Online (Sandbox Code Playgroud)

我想让第三个案例运行(表明它是错的)

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

更多信息在这里

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

dka*_*zel 6

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

您的解决方案是将每个断言放入不同的测试中.

另外一个注意事项,通常断言的第一个参数是期望值,所以我交换输入.

@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));
}
Run Code Online (Sandbox Code Playgroud)

  • 问题是重复的,所以我不会将其作为答案发布,但在某些情况下,在单个测试用例中保留多个断言可能是合适的但是使用JUnit的[ErrorCollector](http://junit.org/apidocs/ org/junit/rules/ErrorCollector.html)或AssertJ的[soft assertions](http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#soft-assertions). (2认同)