如何将features文件夹动态传递到Jenkins-> Maven-> Testng Runner文件中

问题描述 投票:1回答:1

在我的詹金斯管道中,我正在调用测试,例如

"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"
) 
maven jenkins cucumber testng
1个回答
0
投票

有两种方法可以满足您的要求。

  1. 您可以使用Jenkins的build参数并传递run_location。然后,使用在maven构建之前运行的Execute Shell脚本选项,可以使用sed命令将此参数添加到pom.xml文件,功能文件和testng.xml文件中。 sed命令用新文本替换现有文本。您可以在Google上搜索有关sed命令的用法。
  2. 第二个选项是将运行位置作为Jenkins构建参数传递。然后在您的Java代码中接收build参数。然后将此运行位置作为参数动态添加到您的testng xml文件中。这称为testng的程序执行。下面的代码将执行此操作。以下代码读取现有的testng XML文件,并根据您的Jenkins输入添加一个参数。这样,您可以使用下面提到的代码在任何测试中接收此值。

但是这种方法不会修改您的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**/
   }

如果以上提供的解决方案解决了您的问题,请您接受我的回答。

© www.soinside.com 2019 - 2024. All rights reserved.