简体   繁体   English

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

[英]Maven-surefire-plugin with TestNg : how to specify the directory where test suites xml files are stored?

I'm currently working on a Maven-backed project. 我目前正在开发Maven支持的项目。 I've chosen TestNg to implement my unitary tests. 我选择了TestNg来实现我的单一测试。 In order to run my unitary tests at each Maven build, I have added the maven-surefire-plugin to my pom.xml : 为了在每个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>

Moreover, I want to specify the tests to be executed using TestNg's TestSuiteXmlFile. 此外,我想指定使用TestNg的TestSuiteXmlFile执行的测试。 For instance, in my pom.xml, I've configured the surefire plugin so that it will execute the tests defined in the xml file named "testsuite-persistence-layer.xml". 例如,在我的pom.xml中,我配置了surefire插件,以便它将执行名为“testsuite-persistence-layer.xml”的xml文件中定义的测试。

The problem is that by default, the surefire plugin seems to be looking for this xml file at the root of my project. 问题是默认情况下,surefire插件似乎在我的项目的根目录中寻找这个xml文件。 How can I specify the directory in which the surefire plugin should look for the TestSuite xml files? 如何指定surefire插件应该在哪个目录中查找TestSuite xml文件?

According to the TestNg documenation, this could be specified through the "maven.testng.suitexml.dir" property but the Surefire plugin does not seem to take it into account. 根据TestNg文档,这可以通过“maven.testng.suitexml.dir”属性指定,但Surefire插件似乎没有考虑到它。

I am not sure if I understand your problem. 我不确定我是否理解你的问题。 You can easily specify the exact location of xml file, both as relative and fully qualified path. 您可以轻松指定xml文件的确切位置,包括相对路径和完全限定路径。

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

or 要么

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

But that's too easy, so I am guessing you are looking for a way to parametrize the directory where xmls are located. 但这太容易了,所以我猜你正在寻找一种方法来参数化xmls所在的目录。 The quick solution that comes to my mind would be 我想到的快速解决方案就是

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

Now you can run 现在你可以跑了

mvn test -DxmlPath=c:/some/path

Of course xmlPath is just made-up name, you can use any other variable name you wish. 当然xmlPath只是一个虚构的名称,你可以使用你想要的任何其他变量名。

If you don't want to pass the path as an argument from command line you can specify the value of xmlPath variable in properties section of your POM. 如果您不想从命令行将路径作为参数传递,则可以在POM的属性部分中指定xmlPath变量的值。 Properties is one of the main sections located just under < project > branch. 属性是位于<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使 maven-surefire-plugin 与 Eclipse 中的 TestNG 一起工作 - How to make maven-surefire-plugin work with TestNG in Eclipse 使用Maven运行testng套件xml时,maven-surefire-plugin中的NullPointerException - NullPointerException in maven-surefire-plugin when running testng suite xml with maven JUnit 和 Spock 测试与 maven-surefire-plugin - JUnit and Spock test with maven-surefire-plugin 从maven运行TestNG套件获取错误:maven-surefire-plugin:test failed:testSuiteXmlFiles0具有null值 - Run TestNG suite from maven getting error:maven-surefire-plugin:test failed: testSuiteXmlFiles0 has null value 如何使用在docker中运行的mysql运行maven测试(使用maven-surefire-plugin) - How to run maven test with mysql running in docker (using maven-surefire-plugin) 带有Maven-surefire-plugin的UnsatisfiedLinkError - UnsatisfiedLinkError with maven-surefire-plugin 如何参数化Maven surefire插件,以便我可以选择运行哪些TestNG套件 - How to parametrize Maven surefire plugin so I can choose which TestNG suites to run maven-surefire-plugin包含单一测试不起作用 - maven-surefire-plugin inclusion of Single test is not working 运行 maven-surefire-plugin:test 时出现 lombok 编译错误 - lombok compilation errors when running maven-surefire-plugin:test 如何在控制台上显示maven-surefire-plugin单元测试覆盖率报告 - How to show maven-surefire-plugin unit test coverage report on console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM