Closed jedwards1211 closed 7 years ago
Hello,
"doesn't seem to work" is not very informative nor constructive. What do you expect to happen? What did you see? What is it you are trying to achieve?
As per TestNG documentation, @Parameters is to be used with test methods (annotated with @Test). So I really do not understand what you are trying to achieve.
Please come back with a use case or close the issue. Cheers.
The convention you have shown doesn't work for DataProviders. You can create DataProviders that takes Method
as argument as suggested in the documentation. Alternatively you can use Dependency Injection and grab the ITestContext
and fetch parameters, again as suggested in the documentation.
Any @DataProvider can declare a parameter of type ITestContext or java.lang.reflect.Method. The latter parameter will receive the test method that is about to be invoked.
This can be done using the below approach (Used TestNG 6.12)
Test class
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Issue476TestClass {
@Parameters({"symbols", "strategy-types"})
@DataProvider
public Object[][] dp(ITestContext context) {
//This parameter is specified at the <suite> level
String symbols = context.getCurrentXmlTest().getSuite().getParameter("symbols");
//This parameter is specified at the <test> level
String strategyTypes = context.getCurrentXmlTest().getParameter("strategy-types");
System.err.println("Symbol : " + symbols);
System.err.println("Strategy types : " + strategyTypes);
return new Object[][]{
{0}
};
}
@Test(dataProvider = "dp")
public void test(int i) {
System.err.println("Test value ::" + i);
}
}
Test runner
import org.testng.TestNG;
import org.testng.annotations.Test;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class Issue476TestRunner {
@Test
public void testMethod() {
XmlSuite suite = new XmlSuite();
Map<String, String> suiteParams = new HashMap<>();
suiteParams.put("symbols", "usd");
suite.setName("suite");
suite.setParameters(suiteParams);
XmlTest test = new XmlTest(suite);
Map<String, String> testParams = new HashMap<>();
testParams.put("strategy-types", "game");
test.setParameters(testParams);
test.setClasses(Collections.singletonList(new XmlClass(Issue476TestClass.class)));
TestNG testng = new TestNG();
testng.setXmlSuites(Collections.singletonList(suite));
testng.setVerbose(2);
testng.run();
}
}
Output
Symbol : usd
Strategy types : game
Test value ::0
PASSED: test(0)
===============================================
Command line test 06aff0b9-f5dc-4f22-b1a8-702e3612b59b
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
The following doesn't seem to work: