Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run selected junit tests with different parameters

I want to run selected test methods from any test class with different parameters

Ex: 1) ClassA -> Test methods A, B

@Test
public void testA(String param) {
    System.out.println("From A: "+param);
}
@Test
public void testB(String param) {
}

2) ClassB -> Test methods C, D

@Test
public void testC(String param) {
    System.out.println("From C: "+param);
}
@Test
public void testD(String param) {
}

From these I want to run following tests
1) testA(from ClassA) two times with diff params "test1" & "test2"
2) testC(from ClassB) two times with diff params "test3" & "test3"

Here my test count should show as '4'

Can anyone help on this...

like image 559
kvysh Avatar asked May 11 '15 06:05

kvysh


People also ask

Which annotation in JUnit 5 is to run a test multiple times with different arguments?

Parameterized tests make it possible to run the same test multiple times with different arguments. This way, we can quickly verify various conditions without writing a test for each case. We can write JUnit 5 parameterized tests just like regular JUnit 5 tests but have to use the @ParameterizedTest annotation instead.

Does JUnit support parameterized tests where tests can be executed multiple times with different parameter values?

JUnit5 Parameterized Test helps to run the same tests multiple times with different arguments/values. They are declared just like the regular @Test method but instead of @Test annotation @ParameterizedTest is used. All supported argument sources are configured using annotations from the org.


2 Answers

Use Parameterized tests provided with Junits, wherein you can pass the parameters at run time.

Refer to org.junit.runners.Parameterized (JUnit 4.12 offers the possibility to parametrize with expected values and without expected values in the setup array).

Try this:

@RunWith(Parameterized.class)
public class TestA {

   @Parameterized.Parameters(name = "{index}: methodA({1})")
   public static Iterable<Object[]> data() {
      return Arrays.asList(new Object[][]{
            {"From A test1", "test1"}, {"From A test2", "test2"}
      });
   }

   private String actual;
   private String expected;

   public TestA(String expected,String actual) {
      this.expected = expected;
      this.actual = actual;
   }

   @Test
   public void test() {
      String actual = methodFromA(this.actual);
      assertEquals(expected,actual);
   }

   private String methodFromA(String input) {
      return "From A " + input;
   }
}

you can write a similar test for class B.

For a test taking just single parameters, fro JUnit 4.12 you can do this:

@RunWith(Parameterized.class)
public class TestU {

    /**
     * Provide Iterable to list single parameters
     */

    @Parameters
    public static Iterable<? extends Object> data() {
        return Arrays.asList("a", "b", "c");
    }

    /**
     * This value is initialized with values from data() array
     */

    @Parameter
    public String x;

    /**
     * Run parametrized test
     */

    @Test
    public void testMe() {
        System.out.println(x);
    }
}
like image 97
shivani kaul Avatar answered Sep 22 '22 12:09

shivani kaul


Try using JUnit parameterized testing. Here is a tutorial from TutorialsPoint.com:

JUnit 4 has introduced a new feature called parameterized tests. Parameterized tests allow a developer to run the same test over and over again using different values. There are five steps that you need to follow to create a parameterized test.

  • Annotate test class with @RunWith(Parameterized.class).

  • Create a public static method annotated with @Parameters that returns a Collection of Objects (as Array) as test data set.

  • Create a public constructor that takes in what is equivalent to one "row" of test data.

  • Create an instance variable for each "column" of test data.

  • Create your test case(s) using the instance variables as the source of the test data.

The test case will be invoked once for each row of data.

like image 28
amith Avatar answered Sep 21 '22 12:09

amith