Junit4和TestNG在Maven的一个项目中
要将它们一起运行,可用的选项很少,但我选择了为Junit和TestNG使用不同的配置文件。 但现在的问题是排除和包括测试用例。
因为如果我们在maven中将testNG依赖项添加到主项目,它将跳过所有Junit,我已经决定将它放在单独的配置文件中。 所以我在默认(主)配置文件中排除TestNG测试,使用pom.xml中的以下条目进行编译:
org.apache.maven.plugins maven-compiler-plugin 2.0.2 1.5 1.5 **/tests/**.* **/tests/utils/**.*
和surefire插件相同。 所以它适用于主配置文件并且只执行Junit4测试。 但是当我使用testNG配置文件时,它不会执行testNG测试,即使它不会编译它们。 我正在使用以下配置文件来执行它们。
testNG org.apache.maven.plugins maven-compiler-plugin 1.5 1.5 **/tests/**.java **/tests/utils/**.* org.apache.maven.plugins maven-surefire-plugin false **/**.class **/tests/utils/**.class org.testng testng 5.8 test jdk15
任何人都知道为什么它不包括它们并再次编译?
编译器插件的配置排除了TestNG类型。 配置文件中的配置与默认配置合并,因此您的有效编译器配置为:
org.apache.maven.plugins maven-compiler-plugin 1.5 1.5 **/tests/**.java **/tests/utils/**.* **/tests/**.* **/tests/utils/**.*
这意味着您的TestNG类型不会被编译,因此不会运行。
如果在testNG配置文件中指定
dummy
最简单的解决方案是这样的
maven-surefire-plugin ${surefire.version} org.apache.maven.surefire surefire-junit47 ${surefire.version} org.apache.maven.surefire surefire-testng ${surefire.version}
有关此内容的更多信息: 在一个Maven模块 – 2013版中混合TestNG和JUnit测试
我没有在Surefire和Failsafe上找到任何在线组合解决方案。 下面的POM文件更改对我有用。
- Surefire技巧是通过为JUnit和TestNG显式指定子插件来完成的。
- Failsafe技巧是通过定义两个执行来完成的,每个执行都有一个空配置,一个用于JUnit,另一个用于TestNG。
这两种解决方案都是我在网上找到的黑客。 我认为Surefire技巧的链接是对这个问题的另一个答案。
maven-surefire-plugin ${surefire.version} org.apache.maven.surefire surefire-junit47 ${surefire.version} org.apache.maven.surefire surefire-testng ${surefire.version} org.apache.maven.plugins maven-failsafe-plugin ${failsafe.version} test-testng integration-test verify none:none test-junit integration-test verify none:none