Java Maven surefire插件和TestNg:如何指定存储测试套件xml文件的目录?

Java Maven surefire插件和TestNg:如何指定存储测试套件xml文件的目录?,java,unit-testing,testng,maven-surefire-plugin,Java,Unit Testing,Testng,Maven Surefire Plugin,我目前正在从事一个由Maven支持的项目。我选择TestNg来实现我的单一测试。为了在每个Maven构建中运行单一测试,我将Maven surefire插件添加到pom.xml中: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId&g

我目前正在从事一个由Maven支持的项目。我选择TestNg来实现我的单一测试。为了在每个Maven构建中运行单一测试,我将Maven surefire插件添加到pom.xml中:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12</version>
            <configuration>
            <!-- Configuring the test suites to execute -->
                <suiteXmlFiles>
                     <suiteXmlFile>testsuite-persistence-layer.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>

org.apache.maven.plugins
maven surefire插件
2.12
testsuite-persistence-layer.xml
此外,我想使用TestNg的TestSuiteXmlFile指定要执行的测试。例如,在我的pom.xml中,我配置了surefire插件,以便它执行名为“testsuite persistence layer.xml”的xml文件中定义的测试

问题是,默认情况下,surefire插件似乎在我的项目根目录下查找此xml文件如何指定surefire插件查找TestSuite xml文件的目录?


根据TestNg文档,这可以通过“maven.TestNg.suitexml.dir”属性指定,但Surefire插件似乎没有考虑到这一点。

我不确定是否理解您的问题。您可以轻松地指定xml文件的确切位置,包括相对路径和完全限定路径

<suiteXmlFile>c:/some/dir/testsuite-persistence-layer.xml</suiteXmlFile>
当然,xmlPath只是一个虚构的名称,您可以使用任何其他您想要的变量名

如果不想从命令行将路径作为参数传递,可以在POM的properties部分指定xmlPath变量的值。属性是位于分支下的主要部分之一

<project ... >
    ...

    <properties>
        <xmlPath>c:/some/path</xmlPath>
    </properties>
        ...

    <build>
        ...
        <plugins>
        ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>${xmlPath}/testSuite.xml</suiteXmlFile>
                    </suiteXmlFiles>                                        
                    ...
                </configuration>
            </plugin>
        ...
        </plugins>
        ...
    </build>
    ...

</project>

...
c:/some/path
...
...
...
org.apache.maven.plugins
maven surefire插件
2.9
${xmlPath}/testSuite.xml
...
...
...
...

非常感谢您的帮助!不知道也没有尝试在标记中指定相对于项目根的路径,因此另一种方法是在命令集下一个接一个地运行mvn-D测试${path}/test1测试,mvn-D测试${path}/test2测试和依赖关系是org.apache.maven.plugins maven surefire plugin 2.12${tests}.xml
<suiteXmlFile>${xmlPath}/testSuite.xml</suiteXmlFile>
mvn test -DxmlPath=c:/some/path
<project ... >
    ...

    <properties>
        <xmlPath>c:/some/path</xmlPath>
    </properties>
        ...

    <build>
        ...
        <plugins>
        ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>${xmlPath}/testSuite.xml</suiteXmlFile>
                    </suiteXmlFiles>                                        
                    ...
                </configuration>
            </plugin>
        ...
        </plugins>
        ...
    </build>
    ...

</project>