umitems umitems - 2 months ago 12
Android Question

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

I just begin to try on JUnit. I have created some test cases. However, the test case will stop when I found any wrong test case. I want to run through every test case even there are many wrong test case.

e.g.

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


I want to let the third this case be run (show that it is wrong)

Answer

Assertions are not test cases. A failed assertion will throw an exception which if uncaught will propagate up and the rest of the test will not be executed.

Your solution is to put each of these assertions into different tests.

Also a side note, usually the first argument to the assertion is the expected value so I'd swap the inputs.

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