简体   繁体   中英

How can I run many test cases for a test method

I'm using JUnit.I have a test method to test a method and some test cases. I want to run all tes case in that test method, but I can't do that. When first test case fail, test method don't run second test case

Here is my code

public class ComputeServiceTest extends TestCase {

//test add method
public void testAdd()
{
    ComputeServices instance = new ComputeServices();

    //First test case 
    int x1 = 7;
    int y1 = 5;

    int expResult1 = 13;
    int result1 = instance.add(x1, y1);
    assertEquals("First test case fail",expResult1, result1);


    // Second test case
    int x2 = 9;
    int y2 = 6;

    int expResult2 = 15;
    int result2 = instance.add(x2, y2);
    assertEquals("Second test case fail",expResult2, result2);


}

How can I do that?

The standard advice here would be to put your second test case into a separate method, then it will run regardless of whether or not the first "test case" succeeds or not.

You can use a setUp method to initialize the ComputeServices instance so you don't need that boilerplate in each test method.

A test case aborts at the first assertion failure, this is by design.

This is to isolate the test cases: if your second assertion fails, how would you know if instance.add(9, 6) is broken, or if this has been caused by the first invocation of the add() method ?

This is useful when the method under test returns an object, or NULL in case of an error. The first assertion ensures the method returned an object, and then it is possible to invoke methods of that object to verify its state. When NULL is returned, the test case aborts on the first assertion an no NullPointerException will be thrown (the pattern is named guard assertion ).

It is possible to have as many test methods in a TestCase.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM