[英]Junit4 and TestNG in one project with Maven with inheritance
I have the same problem as Junit4 and TestNG in one project with Maven but I still have issues. 我在与Maven一起的一个项目中遇到了与Junit4和TestNG相同的问题,但仍然有问题。 I created simple project where I need to use junit4 and testng Here is root pom:
我创建了一个简单的项目,需要在其中使用junit4和testng这是root pom:
<groupId>com.dimas.testmodule</groupId>
<artifactId>TheParent</artifactId>
<version>0.0.001</version>
<name>TheParent</name>
<packaging>pom</packaging>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
<spring.scope>test</spring.scope>
<maven.surefire.plugin.version>2.9</maven.surefire.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
...etc
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>JUnitOnly</module>
<module>TestNGOnly</module>
<module>testmodule</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
<excludes>
<exclude>**/*NGTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>testNGonly</id>
<modules>
<module>JUnitOnly</module>
<module>TestNGOnly</module>
<module>testmodule</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
<includes>
<include>**/*NGTest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
modules JUnitOnly and TestModule just inherits and do not overrite any properties. JUnitOnly和TestModule模块只是继承而已,不会覆盖任何属性。 And here are TestNgOnly
这是TestNgOnly
<groupId>com.dimas.testmodule.testngonly</groupId>
<artifactId>TestNGOnly</artifactId>
<version>0.0.001</version>
<name>testngonly</name>
<parent>
<groupId>com.dimas.testmodule</groupId>
<artifactId>TheParent</artifactId>
<version>0.0.001</version>
</parent>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.3.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>testNGtest</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
<suiteXmlFiles>
<suiteXmlFile>
<!--test/resources/my-testng.xml-->
src\test\resources\my-testng.xml
</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Well I need to ignore testng tests for default profile and launch only testng tests for profile testNGOnly. 好吧,我需要忽略默认配置文件的testng测试,只启动配置文件testNGOnly的testng测试。
There are three modules - junit
, testng
and mixed
. 有三个模块-
junit
, testng
和mixed
。
The default profile should execute only junit tests - ie run junit
module and run junit tests of mixed
module. 默认配置文件应仅执行junit测试-即运行
junit
模块并运行mixed
模块的junit测试。 testNG
profile should execute only testNG tests - ie run testNG
module and run testNG tests of mixed
module. testNG
配置文件应仅执行testNG测试-即运行testNG
模块和运行mixed
模块的testNG测试。
The way profiles work in maven, it is not possible to exclude
a module in a profile. 概要文件在Maven中的工作方式,无法在概要文件中
exclude
模块。 However, there is no need to build a module which is not required. 但是,无需构建不需要的模块。
Thus, in default profile, have modules junit
and mixed
. 因此,在默认配置文件中,具有模块
junit
和mixed
。
In testNG profile, add module testNG
. 在testNG配置文件中,添加模块
testNG
。
Following maven best practice, define <dependencies>
in the parent pom within <dependencyManagement>
. 遵循maven最佳实践,在
<dependencies>
中的父pom中定义<dependencyManagement>
。 This allows us to only use junit dependency in junit
module and testNG dependency in testNG
module. 这使我们只能在
junit
模块中使用junit依赖关系,而在testNG
模块中仅使用testNG依赖关系。
There is nothing common about surefire
configuration across the three modules and as such not required to be specified in parent pom. 三个模块之间的
surefire
配置没有什么共通之处,因此不需要在父pom中指定。
Since it not required to run junit tests in testNG
profile, add a plugin configuration to skipTests
in testNG
profile for junit
module. 由于它不需要在运行JUnit测试
testNG
配置文件,添加一个插件配置skipTests
在testNG
配置文件junit
模块。
Configure the mixed
pom using the tip in this answer for the related SO question. 使用此答案中的提示针对相关的 SO问题配置
mixed
pom。 This gets tricky since surefire needs to use junit or testng runner based on the profile. 这很棘手,因为surefire需要根据配置文件使用junit或testng运行程序。
I have incorporated the above suggestions in a sample project here . 我将以上建议纳入了此处的示例项目中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.