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

kyi*_*yiu 3 java testng unit-testing maven-surefire-plugin

我目前正在开发Maven支持的项目.我选择了TestNg来实现我的单一测试.为了在每个Maven构建中运行我的单一测试,我已经将maven-surefire-plugin添加到我的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>
Run Code Online (Sandbox Code Playgroud)

此外,我想指定使用TestNg的TestSuiteXmlFile执行的测试.例如,在我的pom.xml中,我配置了surefire插件,以便它将执行名为"testsuite-persistence-layer.xml"的xml文件中定义的测试.

问题是默认情况下,surefire插件似乎在我的项目的根目录中寻找这个xml文件.如何指定surefire插件应该在哪个目录中查找TestSuite xml文件?

根据TestNg文档,这可以通过"maven.testng.suitexml.dir"属性指定,但Surefire插件似乎没有考虑到它.

Jac*_*ekM 6

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

<suiteXmlFile>c:/some/dir/testsuite-persistence-layer.xml</suiteXmlFile>
Run Code Online (Sandbox Code Playgroud)

要么

<suiteXmlFile>src/test/java/com/something/project/testsuite-persistence-layer.xml</suiteXmlFile>
Run Code Online (Sandbox Code Playgroud)

但这太容易了,所以我猜你正在寻找一种方法来参数化xmls所在的目录.我想到的快速解决方案就是

<suiteXmlFile>${xmlPath}/testSuite.xml</suiteXmlFile>
Run Code Online (Sandbox Code Playgroud)

现在你可以跑了

mvn test -DxmlPath=c:/some/path
Run Code Online (Sandbox Code Playgroud)

当然xmlPath只是一个虚构的名称,你可以使用你想要的任何其他变量名.

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

<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>
Run Code Online (Sandbox Code Playgroud)