如何将功能文件夹动态传递到 Jenkins->Maven->Testng Runner 文件
How to Pass the features folder dynamically to a Jenkins->Maven->Testng Runner File
在我的詹金斯管道中,我正在调用我的测试
"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"
)
有两种方法可以达到您的要求。
- 您可以使用 Jenkins 的构建参数并传递您的 run_location。然后使用 Maven 构建之前 运行s 的执行 Shell 脚本选项,您可以使用 sed 命令将此参数添加到您的 pom.xml文件、功能文件和 testng.xml 文件。 sed 命令用新文本替换现有文本。关于sed命令的用法,您可以搜索google。
- 第二个选项是将 运行-位置作为 Jenkins 构建参数传递。然后在 java 代码中接收构建参数。然后将此 运行-location 作为参数动态添加到您的 testng xml 文件中。这称为 testng 的程序化执行。下面的代码将执行此操作。下面的代码读取现有的 testng XML 文件并根据您的 Jenkins 输入添加一个参数。这样,您可以使用下面提到的代码在任何测试中接收此值。
但是这种方法不会修改你的pom.xml文件和特征文件。在执行期间,您必须想办法使用 java.
将此值添加到您的代码中
使用动态 jenkins 参数以编程方式执行 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**/
}
在我的詹金斯管道中,我正在调用我的测试
"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"
)
有两种方法可以达到您的要求。
- 您可以使用 Jenkins 的构建参数并传递您的 run_location。然后使用 Maven 构建之前 运行s 的执行 Shell 脚本选项,您可以使用 sed 命令将此参数添加到您的 pom.xml文件、功能文件和 testng.xml 文件。 sed 命令用新文本替换现有文本。关于sed命令的用法,您可以搜索google。
- 第二个选项是将 运行-位置作为 Jenkins 构建参数传递。然后在 java 代码中接收构建参数。然后将此 运行-location 作为参数动态添加到您的 testng xml 文件中。这称为 testng 的程序化执行。下面的代码将执行此操作。下面的代码读取现有的 testng XML 文件并根据您的 Jenkins 输入添加一个参数。这样,您可以使用下面提到的代码在任何测试中接收此值。
但是这种方法不会修改你的pom.xml文件和特征文件。在执行期间,您必须想办法使用 java.
将此值添加到您的代码中使用动态 jenkins 参数以编程方式执行 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**/
}