简体   繁体   中英

JUnit: Run one test with different configurations

I have 2 test methods, and i need to run them with different configurations

myTest() {
    .....
    .....
}

@Test
myTest_c1() {
    setConf1();
    myTest();
}

@Test
myTest_c2() {
    setConf2();
    myTest();
}

//------------------

nextTest() {
    .....
    .....
}

@Test
nextTest_c1() {
    setConf1();
    nextTest();
}

@Test
nextTest_c2() {
    setConf2();
    nextTest();
}

I cannot run them both from one config (as in code below) because i need separate methods for tosca execution.

@Test
tests_c1() {
    setConf1();
    myTest()
    nextTest();
}

I don't want to write those 2 methods to run each test, how can i solve this?

First i thought to write custom annotation

@Test
@RunWithBothConf
myTest() {
   ....
}

But maybe there are any other solutions for this?

What about using Theories ?

@RunWith(Theories.class)
public class MyTest{

   private static enum Configs{
     C1, C2, C3;
   }

  @DataPoints
  public static Configs[] configValues = Configs.values();

  private void doConfig(Configs config){
    swich(config){...}
  }

  @Theory
  public void test1(Config config){
      doConfig(config);

      // rest of test
  }

  @Theory
  public void test2(Config config){
     doConfig(config);

      // rest of test
  }

Not sure why formatting if off.

Here is how I would approach it:

  • Create two test classes
  • The first class configures to conf1 but uses the @Before attribute trigger the setup
  • The second class extends the first but overrides the configure method

In the example below I have a single member variable conf . If no configuration is run it stays at its default value 0. setConf1 is now setConf in the Conf1Test class which sets this variable to 1. setConf2 is now setConf in the Conf2Test class.

Here is the main test class:

public class Conf1Test
{
   protected int conf = 0;

   @Before
   public void setConf()
   {
      conf = 1;
   }
   @Test
   public void myTest()
   {
      System.out.println("starting myTest; conf=" + conf);
   }

   @Test
   public void nextTest()
   {
      System.out.println("starting nextTest; conf=" + conf);
   }
}

And the second test class

public class Conf2Test extends Conf1Test
{
   // override setConf to do "setConf2" function
   public void setConf()
   {
      conf = 2;
   }
}

When I configure my IDE to run all tests in the package I get the following output:

starting myTest; conf=1
starting nextTest; conf=1
starting myTest; conf=2
starting nextTest; conf=2

I think this gives you what. Each test only has to be written once. Each test gets run twice, once with conf1 and once with conf2

I have a similar issue in a bunch of test cases I have, where certain tests need to be run with different configurations. Now, 'configuration' in your case might be more like settings, in which case maybe this isn't the best option, but for me it's more like a deployment model, so it fits.

  1. Create a base class containing the tests.
  2. Extend the base class with one that represents the different configuration.
  3. As you execute each of the derived classes, the tests in the base class will be run with the configuration setup in its own class.
  4. To add new tests, you just need to add them to the base class.

The way you have it right now seems fine to me. You aren't duplicating any code, and each test is clear and easy to understand.

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