java - 如何使 maven-surefire-plugin 与 Eclipse 中的 TestNG 配合使用

标签 java maven testng maven-surefire-plugin

我无法让这两个在 Eclipse 中一起工作

我可以像这样设置surefire插件

<suiteXmlFiles>
        <suiteXmlFile>${basedir}/src/test/resources/testng.xml</suiteXmlFile>
    </suiteXmlFiles>
    <testResources>
        <testResource>
            <directory>${project.basedir}/src/test/java</directory>
            <includes>
                <include>**/*.*</include>
            </includes>
        </testResource>
        <testResource>
            <directory>${project.basedir}/src/test/resources</directory>
            <includes>
                <include>**/*</include>
            </includes>
        </testResource>
    </testResources>

然后运行$mvn test所有资源均已编译并移至/target/test-classes ,效果很好。

但是如果我运行 $mvn clean然后使用TestNG View 运行一些测试,/target/test-classes中没有编译任何内容,这样我要么必须在此之前运行测试阶段,要么以某种方式使用 maven-compiler-plugin:testCompile目标是编译测试资源...

但是maven-compiler-plugin:2.3.2:testCompile不像 Surefire 插件那样通过 testResources 设置配置。它只是将测试源代码编译成测试类

我应该怎么做才能让 testNG 像运行 Surefire 插件一样执行?我不知何故需要 <testResources> 产生的效果

最佳答案

testResources 和 resourcesmaven-resources-plugin 的一部分

编译和测试编译maven-compiler-plugin的一部分

因此,您所要做的就是在通过 TestNG View 运行测试之前单击一个调用所有这些阶段和目标的按钮

compile 
test-compile
maven-resources-plugin:resources 
maven-resources-plugin:testResources 

此外,这一点非常重要,每当您从 TestNG View 运行测试时,Eclipse 都会使用您单击的测试(一个测试或一类测试)创建一个临时 testng.xml 定义。因此,如果您通过 Eclipse 的运行方式使用 testNG 运行配置,则通过 SureFire 插件运行时在 testng.xml 中拥有的其他设置会丢失......它看起来像这样

/tmp/testng-eclipse-388280625/testng-customsuite.xml

-

<suite name="Default suite">
  <test verbose="2" name="Default test">
    <classes>
      <class name="com.example.tests.selenium.SubmitUploadFormTest"/>
    </classes>
  </test>
</suite>

它有一个方便的方法来处理这个问题 - 在 eclipse testng 首选项中设置模板 testng.xml 文件。

关于java - 如何使 maven-surefire-plugin 与 Eclipse 中的 TestNG 配合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6233711/

相关文章:

java - TestNG 失败并不是失败

java - 当焦点改变时如何改变经理的字段

java - 实现具有相同功能的 Controller 的最佳实践是什么?

maven - 在Gradle中运行从Maven下载的Java Artifact

selenium - 将 Testng 与 cucumber 一起使用有什么好处吗?

java - TestNG 重试类不运行 @BeforeClass 和 @AfterClass 方法

java - 用于生成 Java JNA 包装器的 jnaerator 的 SimpleMeaningfulExample 将不起作用

java - 获取应用程序的端口号

java - Maven 编译器插件总是将一组源检测为 "stale"

java - 使用依赖项构建 Maven