Junit4 和 TestNG 位于一个带有 Maven 的项目中
要一起运行它们,可用的选项很少,但我选择为 Junit 和 TestNG 使用不同的配置文件。 但现在的问题是排除和包含测试用例。
因为如果我们将 testNG 依赖项添加到 Maven 中的主项目中,它将跳过所有 Junit,我决定将其放在单独的配置文件中。 因此,我使用 pom.xml 中的以下条目排除默认(主)配置文件中的 TestNG 测试:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<testExcludes>
<testExclude>**/tests/**.*</testExclude>
<testExclude>**/tests/utils/**.*</testExclude>
</testExcludes>
</configuration>
</plugin>
对于 Surefire 插件也是如此。 因此它可以与主配置文件配合使用,并且仅执行 Junit4 测试。 但是当我使用 testNG 配置文件时,它不会执行 testNG 测试,即使它不会编译它们。 我正在使用以下配置文件来执行它们。
<profile>
<id>testNG</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<testIncludes>
<testInclude>**/tests/**.java</testInclude>
<testInclude>**/tests/utils/**.*</testInclude>
</testIncludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<includes>
<include>**/**.class</include>
<include>**/tests/utils/**.class</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.8</version>
<scope>test</scope>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
</profile>
有人知道为什么它不包括它们并再次编译吗?
To run them together there are few options available but I have chosen using different profiles for Junit and TestNG. But now problem is with excluding and including test cases.
Since if we add testNG dependency to main project in maven it will skip all Junit,I have decided to put it in separate profile. So I am excluding TestNG tests in default(main) profile from compiling using following entry in pom.xml :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<testExcludes>
<testExclude>**/tests/**.*</testExclude>
<testExclude>**/tests/utils/**.*</testExclude>
</testExcludes>
</configuration>
</plugin>
and same for surefire plugin. So it works fine with main profile and executes only Junit4 tests. But when I use testNG profile it wont execute testNG test even it wont compile them. I am using following profile to execute them.
<profile>
<id>testNG</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<testIncludes>
<testInclude>**/tests/**.java</testInclude>
<testInclude>**/tests/utils/**.*</testInclude>
</testIncludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<includes>
<include>**/**.class</include>
<include>**/tests/utils/**.class</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.8</version>
<scope>test</scope>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
</profile>
Anybody have any idea why it is not including them and compiling again ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编译器插件的配置不包括 TestNG 类型。 配置文件中的配置与默认配置合并,因此您的有效编译器配置是:
这意味着您的 TestNG 类型从未被编译,因此不会运行。
如果您指定<排除> testNG 配置文件中的部分它将覆盖默认的排除,并且您的 TestNG 类型将被编译和运行。 我不记得它是否适用于空的排除标记(即),您可能必须指定类似的内容以确保覆盖默认配置。
The configuration for the compiler plugin excludes the TestNG types. The configuration from the profile is merged with the default configuration, so your effective compiler configuration is:
This means that your TestNG types aren't ever compiled and therefore aren't run.
If you specify the <excludes> section in the testNG profile it will override the default excludes and your TestNG types will be compiled and run. I can't remember if it will work with an empty excludes tag (i.e. <excludes/>), you may have to specify something like this to ensure the default configuration is overridden.
最简单的解决方案是这样的
更多信息:在一个 Maven 模块中混合 TestNG 和 JUnit 测试 – 2013 版
Simplest solution is like this
More info about this: Mixing TestNG and JUnit tests in one Maven module – 2013 edition
我在网上没有找到任何适用于 Surefire 和 Failsafe 的组合解决方案。 下面的 POM 文件更改对我有用。
这两种解决方案都是我在网上找到的。 我认为 Surefire 技巧的链接在这个问题的另一个答案中。
I didn't find any combined solution online for both Surefire and Failsafe. The POM file changes below worked for me.
Both solutions are hacks that I found online. I think the link to the Surefire trick is in another answer to this question.