Dmitry Glushonkov Dmitry Glushonkov - 2 months ago 14
Java Question

TestNG: Pass parameter to DataProvider from Maven

It seems I tried all the possible ways to achieve this, but no luck. I have a complex code in DataProvider (it really collects data in a complex way), which i want to be eventually parametrized by CI.

The first question here is how to even read parameters inside DataProvider.

1 One way is to get parameters from testng.xml this way:

String lolo = context.getCurrentXmlTest().getParameter("testcases");

But this doesn't solve the problem, because .xml file must be static then, and I want to pass parameters to it from Maven.

2 The other way is to read parameters from @Factory in my case:

String testCaseDataFilename = (method.getAnnotation(Factory.class)).parameters()[0];

Where @Factory is:

@Factory(dataProvider="dataProviderMethod", parameters = {"authentication-testcases.json"})

It works too, but again, doesn't solve the problem, because now parameters must be harcoded in java test files, and my @Factory uses @DataProvider for itself, and it can't receive parameters from Maven implicitly.

Additionally, I have other test parameters, which are passed by Maven, but they are used only in @Test tests, not DataProvider, and @BeforeSuite successfully receives them.

I'll try to simplify:

I am calling maven

-Dtestname=signUp -Dmyparam=1 test

and I want my DataProvider to receive this value of Dmyparam and use it to gather data. All I achieved is receiving this data with @BeforeSuite and setting it globally, but the problem is DataProvider is called before the data is set.


-D is setting a system property.

You'll be able to get it in your code with System.getProperty("MyParam").