在我的詹金斯管道中,我正在调用测试,例如
"mvn test -Drun_location=US"
而且我的pom.xml文件看起来像这样
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<systemPropertyVariables>
<run.location>${run_location}</run.location>
</systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>${basedir}/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<skipTests>false</skipTests>
<testFailureIgnore>false</testFailureIgnore>
</configuration>
</plugin>
而且我的testng.xml文件看起来像这样
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite>
<test>
<parameter name="location" value="${run_location}"/>
<classes>
<class name="com.test.Runner_Jenkins"/>
</classes>
</test>
</suite>
我需要将此参数传递到我的运行程序文件以动态设置功能目录。我该怎么做。
@CucumberOptions (
features = {"features/${run_location}"},
glue = "StepDefinitions"
)
有两种方法可以满足您的要求。
但是这种方法不会修改您的pom.xml文件和功能文件。在执行过程中,您必须找出使用java将这个值添加到代码中的方法。
使用动态詹金斯参数的testng的程序执行:
String run_location = System.getProperty("run_location");
TestNG tng = new TestNG();
File initialFile = new File("testng.xml");
InputStream inputStream = FileUtils.openInputStream(initialFile);
Parser p = new Parser(inputStream);
List<XmlSuite> suites = p.parseToList();
List<XmlSuite> modifiedSuites = new ArrayList<>();
for (XmlSuite suite : suites) {
XmlSuite modifiedSuite = new XmlSuite();
modifiedSuite.setParallel(suite.getParallel());
modifiedSuite.setThreadCount(deviceNames.size());
modifiedSuite.setName(suite.getName());
modifiedSuite.setListeners(suite.getListeners());
List<XmlTest> tests = suite.getTests();
for (XmlTest test : tests) {
for (int i = 0; i < deviceNames.size(); i++) {
XmlTest modifedtest = new XmlTest(modifiedSuite);
HashMap<String, String> parametersMap = new HashMap<>();
parametersMap.put("run_location", run_location);
modifedtest.setParameters(parametersMap);
modifedtest.setXmlClasses(test.getXmlClasses());
}
}
modifiedSuites.add(modifiedSuite);
}
inputStream.close();
tng.setXmlSuites(modifiedSuites);
tng.run();
在测试中访问参数值
@Test
@Parameters(value={"run_location"})
public void executeBeforeTest(String runlocation){
/**Your code goes her**/
}
如果以上提供的解决方案解决了您的问题,请您接受我的回答。