Maven项目单元测试配置问题

来源:互联网 发布:数据库字段唯一性约束 编辑:程序博客网 时间:2024/05/19 17:08

Java语言的Maven项目的单元测试有很多测试框架,常用的有Junit和TestNG。使用不同的测试框架,pom文件需要有不同的配置,配置错误有可能导致执行“mvn test”命令的时候不执行单元测试用例。这里重点说一说Junit和TestNG的pom配置。

  • Junit4 pom配置
    • pom正确配置
    • 注意事项
  • TestNG pom配置
    • 正确配置

Junit4 pom配置

pom正确配置

(1)插件配置

<!-- 测试插件surefire --> <plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-surefire-plugin</artifactId>     <version>2.19.1</version>     <dependencies>         <dependency>             <groupId>org.apache.maven.surefire</groupId>             <artifactId>surefire-junit47</artifactId>             <version>2.19.1</version>         </dependency>     </dependencies> </plugin> <!-- 测试覆盖率 --> <plugin>     <groupId>org.jacoco</groupId>     <artifactId>jacoco-maven-plugin</artifactId>     <version>0.7.6.201602180812</version>     <executions>         <execution>             <id>default-prepare-agent</id>             <goals>                 <goal>prepare-agent</goal>             </goals>         </execution>         <execution>             <id>default-prepare-agent-integration</id>             <goals>                 <goal>prepare-agent-integration</goal>             </goals>         </execution>         <execution>             <id>default-report</id>             <goals>                 <goal>report</goal>             </goals>         </execution>         <execution>             <id>default-report-integration</id>             <goals>                 <goal>report-integration</goal>             </goals>         </execution>         <execution>             <id>default-check</id>             <goals>                 <goal>check</goal>             </goals>             <configuration>                 <rules>                     <rule>                         <element>BUNDLE</element>                         <limits>                             <limit>                                 <counter>COMPLEXITY</counter>                                 <value>COVEREDRATIO</value>                                 <minimum>0.0</minimum>                             </limit>                         </limits>                     </rule>                 </rules>             </configuration>         </execution>     </executions> </plugin>
(2)依赖配置
<dependency>     <groupId>junit</groupId>     <artifactId>junit</artifactId>     <version>4.12</version>     <scope>test</scope> </dependency>

注意事项

(1)注意Junit4测试类命名规范:

在默认情况下,maven-surefire-plugin的test目标会自动执行测试源码路径(默认为src/test/java/)下所有符合一组命名模式的测试类。这组模式为:

**/Test*.java:任何子目录下所有命名以Test开头的Java类。
**/*Test.java:任何子目录下所有命名以Test结尾的Java类。
**/*TestCase.java:任何子目录下所有命名以TestCase结尾的Java类。
正例:TestApp.java, AppTest.java, AppTestCase.java, TestCaseApp.java
反例:AppTests.java

(2)pom中不要配置TestNG

<!-- TestNG -->    <dependency>        <groupId>org.testng</groupId>        <artifactId>testng</artifactId>        <version>6.8.17</version>    </dependency>

TestNG pom配置

正确配置

(1)pom配置

<plugin>   <groupId>org.apache.maven.plugins</groupId>   <artifactId>maven-surefire-plugin</artifactId>   <version>2.12.3</version>   <configuration>        <testFailureIgnore>true</testFailureIgnore>        <suiteXmlFiles>            <suiteXmlFile>testng.xml</suiteXmlFile>        </suiteXmlFiles>    </configuration> </plugin>

(2)testng.xml配置

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"><suite name="interfaceunittest" verbose="2">    <test name="MongoSinkTest" junit="false" thread-count="1" parallel="classes">        <groups>            <run>                <include name="dev"></include>            </run>        </groups>        <packages>            <package name="org.riderzen.flume.sink"></package>        </packages>    </test></suite>

dev为group名称, @Test(groups =”dev”, invocationCount = 1)

0 0
原创粉丝点击