简体   繁体   中英

Is there a way to pass parameters to TestNG test without using @DataProvider & parameters in XML?

Currently, I'm using @DataProvider which reads data from excel and passes it to only one @Test . Generally, this data contains execution flow of a test case, which enables me to run different test cases parallel eliminating the dependency on modifying the testng.xml .

The problem with above approach is I'm not able to prioritize or maintain dependency externally. Is there a way, wherein I can launch @Test contained in TestNG class from another class along with parameters so that I can maintain dependency mentioned in the excel sheet.

@Factory annotated methods are there to help.
You can call constructors of other test classes inside and then access parameters in @Test , @Before* and @After* annotated methods.

public class MainProvider {

    @Factory(dataProvider = "main", dataProviderClass = MainProvider.class)
    public Object[] prepare(ParameterType first, ParameterType second) {
        return new Object[]{new SimpleTest(firstParameter, secondParameter)};
    }

    @DataProvider(name = "main")
    public Iterator<Object[]> dataProvider() {
        List<Object[]> parameters = new ArrayList<>();

        //Fill it with your data (this can be, of course, in a loop):
        parameters.add(new Object[] {parameter1, anotherParameter1});
        parameters.add(new Object[] {parameter2, anotherParameter2});

        return parameters.iterator();
    }
}

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