Maven项目同一个工程(Module)中JUnit与TestNG同时运行
目前应用中,测试代码总体上分为三类:
1.开发人员写的单元测试。此类测试代码通常与开发代码放在同一个工程下,用于测试一些功能方法,业务逻辑,通常使用JUnit做为测试框架。
2.开发人员写的冒烟测试。此类测试代码通常放在Maven项目的xxx-test-test工程下,主要用于交付测试,主要业务逻辑的冒烟测试,一般会使用JUnit做为测试框架
3.测试人员写的集成测试。这类测试用例与脚本一般比较多,使用到的集成测试的框架是在TestNG基础上的。
以上三种测试代码在maven的test阶段执行时需要做以下配置:
总控pom.xml:
[code lang="xml"]
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
[/code]
这个配置指定了所有的单元测试的脚本。此时执行测试mvn test会以执行所有以Test.java结尾的测试代码,而集成测试的脚本也符合这个规则,所以会被执行到。但我们通常会自已去指定一些surefire插件的配置工作,使用上面这个配置显然不够用,下面是我们在xxxx-test-test工程下通常使用的配置:
测试工程xxxx-test-test/pom.xml:
[code lang="xml"]
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
[/code]
上面的配置可以执行集成测试代码,但这样配置之后在xxxx-test-test之下的冒烟测试不会执行,原因是这里的
下面尝试
[code lang="xml"]
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<includes>
<include>**/*Test.java</include>
</includes>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
[/code]
经过测试,这种配置方法只会执行集成测试(TestNG)的内容。这可能是因为在surefire插件的一个执行线程之内只能使用一种方式执行。那么自然想到将JUnit与TestNG两种分开配置,经过几次实验可以得到如下配置成功执行单元测试,冒烟测试与集成测试的内容:
[code lang="xml"]
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4</version>
<configuration>
<!-- 先将所有的测试脚本排除在外,用以覆盖总控pom.xml中的<include>**/*Test.java</include>配置,如果没有这个配置,可能会重复执行一次冒烟测试的内容 -->
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>test-junit</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<testNGArtifactName>none:none</testNGArtifactName>
<!-- 专门配置冒烟测试,必须将冒烟测试(JUnit)的脚本准确包含进来,并且需要包含<excludes>排除掉TestNG脚本,用于覆写第一处的<exclude>**/*.java</exclude>,否则会被<exclude>**/*.java</exclude>影响到而不执行任何JUnit的测试 -->
<includes>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/servicetest/**/*.java</exclude>
</excludes>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</execution>
<execution>
<id>test-testng</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<!-- 专门配置TestNG测试脚本,将需要执行的测试集写在 <suiteXmlFiles> 所指定的xml文件中即可 -->
<configuration>
<junitArtifactName>none:none</junitArtifactName>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</execution>
</executions>
</plugin>
[/code]
以上的配置使用了两个execution,而surefire插件在执行时会分别针对JUnit与TestNG在两个不同的JVM中执行测试用例,所以测试用例不会产生相互影响的现象。
如果想在一个pom.xml配置中同时执行两个不同的JUnit execution,可以采用如下配置:
[code lang="xml"]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- [surefire:test {execution: default-test}] -->
<skipTests>true</skipTests>
</configuration>
<executions>
<execution>
<id>test-no-fork</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skipTests>false</skipTests>
<includes>
<include>**/cases/TC0*.java</include>
</includes>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</execution>
<execution>
<id>test-fork</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skipTests>false</skipTests>
<includes>
<include>**/cases/TC1*.java</include>
</includes>
<forkMode>always</forkMode>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</execution>
<execution>
<id>test-large-jvm</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<parallel>methods</parallel>
<threadCount>10</threadCount>
<argLine>-Xmx1024m</argLine>
<skipTests>true</skipTests>
<includes>
<include>**/cases/TC2*.java</include>
</includes>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</execution>
</executions>
</plugin>
[/code]