如何使 maven-surefire-plugin 与 Eclipse 中的 TestNG 配合使用
我无法让这两个在 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 视图运行一些测试,则 /target/test-classes
中没有编译任何内容,因此我必须在此之前运行测试阶段,或者以某种方式使用 maven-compiler-plugin:testCompile 目标来编译测试资源...
但是 maven-compiler-plugin:2.3.2:testCompile不是这样设置的Surefire 插件通过 testResources
配置。它只是将测试源代码编译成测试类
我应该怎么做才能使 testNG 像运行 Surefire 插件一样执行?我不知何故需要
产生的效果
I cannot make these two work together in eclipse
I can set up surefire plugin like this
<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>
and run $mvn test
and all resources are compiled and moved into /target/test-classes
, which works fine.
But if I run $mvn clean
and then use the TestNG view to run some tests, there is nothing compiled in /target/test-classes
, so that I either have to run test phase before that, or somehow employ maven-compiler-plugin:testCompile
goal to compile test resources ...
but maven-compiler-plugin:2.3.2:testCompile
isn't setup like surefire plugin via the testResources
configuration. It just compiles test source code into test-classes
What should I do to make testNG execute as if I run surefire plugin ? I somehow need the effect that results from <testResources>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
测试资源& resources
是maven-resources-plugin
的一部分compile & test-compile
是maven-compiler-plugin
的一部分,因此您所要做的就是单击一个调用所有这些阶段的按钮。通过 TestNG 视图运行测试之前的目标
另外,这一点非常重要,每当您从 TestNG 视图运行测试时,Eclipse 都会使用您的测试创建一个临时
testng.xml
定义。单击了一个测试或一类测试。因此,如果您通过 Eclipse 的运行方式使用 testNG 运行配置,则通过 SureFire 插件运行时在 testng.xml 中的附加设置会丢失......它看起来像这样-
它有一个方便的方法来处理这个问题 - 设置一个Eclipse testng 首选项中的模板 testng.xml 文件。
testResources & resources
is part ofmaven-resources-plugin
compile & test-compile
is part ofmaven-compiler-plugin
So that all you have to do is to click a button that invokes all these phases & goals before you run tests via TestNG view
Also, and this is very important, whenever you run test(s) from the TestNG view, eclipse creates a temporary
testng.xml
definition with the test(s) you have clicked on, either a test or a class of tests. So that additional settings in testng.xml that you have when running via SureFire plugin gets lost if you are using testNG run configuration via Eclipse's Run As ... it looks like this-
It has a convenient way to deal with this - setting up a template testng.xml file in eclipse testng preferences.