Maven项目单元测试配置问题

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值