2

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?

1
  • Why do you want to write this tests? Commented Oct 25, 2012 at 16:54

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

A little late, anyway. This is the solution I'm using myself, but it seems it's not possible to use this in Junit5. Any thoughts about that?
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.