java - 如何运行所有测试用例,甚至之前的测试用例都是错误的

标签 java android junit 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 );

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

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

更多信息请点击此处

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));
}

关于java - 如何运行所有测试用例,甚至之前的测试用例都是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31226798/

相关文章:

java - 在 Java 中将对象转换为 Double

java - 使用 swagger2Markup 时出现 NoClassDefFoundError

java - 应用小工具 : Ripple effect lost on list item when background is added to outer layout

android - onFinishInflate() 永远不会被调用

android - 如何覆盖 backpress 不杀死 Activity ?

unit-testing - 以编程方式替换PowerMock的@PrepareForTest吗?

java - 在jar中查找静态文件

android - java.lang.SecurityException : Permission Denial: opening provider 错误

java - ND4J JUnit 测试异常

java - 类路径中的 Ant-JUnit 错误 : Ant wants the JUnit . jar