How to run selected junit tests with different parameters
Asked Answered
S

2

10

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...

Sula answered 11/5, 2015 at 6:58 Comment(1)
How are you running your tests. Are you using the IDE or a build tool like Maven?Ashbaugh
K
8

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.

Knocker answered 11/5, 2015 at 7:51 Comment(0)
K
20

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);
    }
}
Knotting answered 11/5, 2015 at 14:45 Comment(1)
This is also correct answer. Need to write code for dynamic parameters in parameterized method and assign to the array.Sula
K
8

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.

Knocker answered 11/5, 2015 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.