Steve Staple
Steve Staple

Reputation: 3279

Converting from JUnit 4 to TestNG. How do I deal with a parameter?

I am trying to convert my already bulging project from JUnit 4 to TestNG, in order to get some decent reports out of it.

I have been following a tutorial at http://test-able.blogspot.co.uk/2016/10/how-to-convert-test-classes-from-junit-to-testng.html

But I do not get this bit at all.

In my existing code, I had the following:

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
@Parameter
public static String browserName;  

@Parameters
public static Collection<Object[]> data () { 
    Common.printLine();

    debug.print(thisClass + " set up browser parameters: " + global.variables.browsers);

    switch(global.variables.browsers) {
        case "all":
            Object[][] data = { {"firefox"}, {"chrome"}, {"edge"} };
            return Arrays.asList(data);   
        case "firefox":
            Object[][] data1 = { {"firefox"}};
            return Arrays.asList(data1); 
        case "chrome":
            Object[][] data2 = { {"chrome"}};
            return Arrays.asList(data2); 
        case "edge":
            Object[][] data3 = { {"edge"}}; 
            return Arrays.asList(data3); 
        default:
            Object[][] data4 =  { {"firefox"}};
            return Arrays.asList(data4); 
    }  
} 

The replacement code for testNG looks fine for the Parameters bit:

    @DataProvider(name = "data")
public static Collection<Object[]> data () { 
    Common.printLine();

    debug.print(thisClass + " set up browser parameters: " + global.variables.browsers);

    switch(global.variables.browsers) {
        case "all":
            Object[][] data = { {"firefox"}, {"chrome"}, {"edge"} };
            return Arrays.asList(data);   
        case "firefox":
            Object[][] data1 = { {"firefox"}};
            return Arrays.asList(data1); 
        case "chrome":
            Object[][] data2 = { {"chrome"}};
            return Arrays.asList(data2); 
        case "edge":
            Object[][] data3 = { {"edge"}}; 
            return Arrays.asList(data3); 
        default:
            Object[][] data4 =  { {"firefox"}};
            return Arrays.asList(data4); 
    }  
} 

This is what is suggested as equivalent for parameter, but I don't understand how to apply it?

@Test(dataProvider="data")
public void test2(String keyword) {

} 

Can anyone explain how to adapt my parameter shown above, browserName?

I think browserName previously held the output from the Collection object, so that when, for instance, the input parameter (global.variables.browsers) was set to 'all', the test would be performed for each of the browsers in order. (as shown here: http://test-able.blogspot.co.uk/2016/03/do-cross-browser-test-automation-like-a-pro-part2.html)

Upvotes: 0

Views: 438

Answers (1)

juherr
juherr

Reputation: 5740

You can use the parameter feature of TestNG:

@Parameters("browserName")
@DataProvider(name = "data")
public static Collection<Object[]> data(String browserName) { 
  // ...
}

With suite:

<suite name="My suite">
  <parameter name="browserName"  value="all"/>
  <test name="Simple example">
  <-- ... -->
  </test>
</suite>

Upvotes: 2

Related Questions