【问题标题】:Maven-surefire-plugin with TestNg : how to specify the directory where test suites xml files are stored?带有 TestNg 的 Maven-surefire-plugin:如何指定存储测试套件 xml 文件的目录?
【发布时间】:2012-05-21 15:12:06
【问题描述】:

我目前正在从事一个由 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 插件似乎没有考虑到这一点。

【问题讨论】:

    标签: java unit-testing testng maven-surefire-plugin


    【解决方案1】:

    我不确定我是否理解您的问题。您可以轻松指定 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 ... >
        ...
    
        <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>
    

    【讨论】:

    • 非常感谢您的帮助!不知道也没有尝试在 标签中指定相对于项目根目录的路径
    • 另一种方法是在另一个 mvn -D 测试 ${path}/test1 test , mvn -D tests ${path}/test2 test 和依赖项之后运行以下命令集 org.apache.maven.pluginsmaven-surefire-plugin2.12${tests}.xml
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 2010-11-16
    相关资源
    最近更新 更多