5

我目前正在开发一个由 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>

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

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

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

4

1 回答 1

7

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

<suiteXmlFile>c:/some/dir/testsuite-persistence-layer.xml</suiteXmlFile>

或者

<suiteXmlFile>src/test/java/com/something/project/testsuite-persistence-layer.xml</suiteXmlFile>

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

<suiteXmlFile>${xmlPath}/testSuite.xml</suiteXmlFile>

现在你可以运行

mvn test -DxmlPath=c:/some/path

当然 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>
于 2012-05-21T19:31:47.110 回答