Maven TestNG项目,将命令行参数传递给testng.xml文件

时间:2017-01-22 05:56:24

标签: maven command-line testng pom.xml

我有一个Maven TestNG项目,我正在尝试将几个命令行参数传递到testng.xml文件中。

testng.xml文件如下所示:

<suite name="Test Suite" parallel="classes" thread-count="100" >
  <test name="VIP Tests">
    <parameter name="browser" value="${browser}" />
    <groups>
        <run>
            <include name="${test.scope}" />
        </run>
    </groups>
    <packages>
        <package name="com.tests.*" />
    </packages>
  </test> 
</suite>

pom.xml文件如下所示:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Test_Project</groupId>
<artifactId>TP_X</artifactId>
<version>1.0-SNAPSHOT</version>
  <build>
    <resources>
        <resource>
            <directory>src/test/resources</directory>
        </resource>
    </resources>
    <sourceDirectory>${src.dir}</sourceDirectory>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <suiteXmlFiles>
                   <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

现在,每次我尝试执行Maven测试命令

mvn clean test -U -Dtest.scope=smoke -Dbrowser=chrome

我在命令行中收到以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project VIP_QE: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test failed: There was an error in the forked process
[ERROR] java.util.regex.PatternSyntaxException:
[ERROR] Illegal repetition near index 0
[ERROR] ${test.scope}

有人可以帮忙吗?我一直坚持这个问题大约一个星期。

1 个答案:

答案 0 :(得分:2)

TestNG和surefire不支持${var}表示法。

但您可以使用the filtering feature of Maven。然后你必须选择修改后的资源:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Test_Project</groupId>
<artifactId>TP_X</artifactId>
<version>1.0-SNAPSHOT</version>
  <build>
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
    <sourceDirectory>${src.dir}</sourceDirectory>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <suiteXmlFiles>
                   <suiteXmlFile>target/test-classes/testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

请注意testResourcesresourcesHow do I filter test resources in maven?

之间的区别