5

I have a base test class for my tests which does the initialisation work before each test.

Here is the code

public class BaseTestParameters {

  MyObj myObj;

  @DataProvider(name = "apiType")   
  public static Object[][] createData() {

     return new Object[][] {{"type", "1"},{"type","2"}};   
  } 

  @BeforeMethod()   
  @Factory(dataProvider = "apiType")   
  public void setup(String type,String param) throws Exception {

     myObj = createMyObject(param);

  }
}

All my test classes extend this base class and they use the myObj for the tests.

myObj has two different ways of creation (depending on param). All the tests will run twice . One with each way of constituting myObj.

How do I enable this scenario ? Using @Factory annotation means I need to return Object[] from that method, but I don't have to return any test classes from that method.

talex
  • 17,973
  • 3
  • 29
  • 66

1 Answers1

5

You can use @Parameters annotation, but you have to specify values in testng,xml it means you have to have separate testng.xml for each set of parameters.

Here is example:

AppTest.java

public class AppTest {
    @Parameters({"par1", "par2"})
    @BeforeMethod()
    public void setUp(String a, String b) {
        System.out.println("a = [" + a + "], b = [" + b + "]");
    }

    @Test
    public void testApp() {
    }
}

testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >

    <test name="Run1" >
        <parameter name="par1"  value="val"/>
        <parameter name="par2"  value="anotherval"/>
        <packages>
            <package name="dummy.java" />
        </packages>
    </test>

    <test name="Run2" >
        <parameter name="par1"  value="newValue"/>
        <parameter name="par2"  value="yetAnotherVal"/>
        <packages>
            <package name="dummy.java" />
        </packages>
    </test>
</suite>
talex
  • 17,973
  • 3
  • 29
  • 66
  • This will just replace par1 and par2 values in setup function. I need testApp to run once with par1 and once with par2. Some kind of iteration over parameters while running the tests. – lostintranslation Mar 22 '19 at 09:45
  • @lostintranslation I updated my answer. Now it runs test twice with different values. – talex Mar 22 '19 at 10:00
  • Getting this error - Parameter 'par1' is required by '@Configuration' on method setup but has not been marked '@Optional' or defined. par2 is overwriting par1 when testng is initialising. – lostintranslation Mar 22 '19 at 10:10
  • Adding two seems overdo for such a simple use case. Will shift to junit. Testng is not a good choice for such cases. – lostintranslation Mar 22 '19 at 10:11
  • How do you run your test? It seems like you didn't use `testng.xml`. – talex Mar 22 '19 at 10:12
  • Yes, `JUnit` support your case way better. – talex Mar 22 '19 at 10:13
  • No. I am using intellij to run my test case. I need to try these parameters for only one test case. Running via xml will run all test cases which I dont want – lostintranslation Mar 22 '19 at 10:23