Junit4和TestNG在Maven的一个项目中 [英] Junit4 and TestNG in one project with Maven
问题描述
要将它们一起运行,可用的选项很少,但我选择为Junit和TestNG使用不同的配置文件。但现在的问题是排除和包括测试用例。
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.
因为如果我们在maven中将testNG依赖项添加到主项目,它将跳过所有Junit,我已经决定将它放入单独的个人资所以我在默认(主要)配置文件中排除了使用pom.xml中的以下条目进行编译时的TestNG测试:
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>
和surefire插件相同。所以它适用于主配置文件并且只执行Junit4测试。但是当我使用testNG配置文件时,它不会执行testNG测试,即使它不会编译它们。我正在使用以下个人资料来执行它们。
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 ?
推荐答案
编译器插件的配置不包括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:
<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>
<testExcludes>
<testExclude>**/tests/**.*</testExclude>
<testExclude>**/tests/utils/**.*</testExclude>
</testExcludes>
</configuration>
</plugin>
这意味着您的TestNG类型不会被编译,因此不会运行。
This means that your TestNG types aren't ever compiled and therefore aren't run.
如果指定< excludes> testNG配置文件中的部分将覆盖默认排除,并且将编译和运行您的TestNG类型。我不记得它是否适用于空的排除标记(即< excludes />),您可能必须指定类似的内容以确保覆盖默认配置。
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.
<testExcludes>
<testExclude>dummy</testExclude>
</testExcludes>
这篇关于Junit4和TestNG在Maven的一个项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!