Maven + testNG + reportNG + Jacoco

本文介绍了如何在Maven项目中结合testNG、reportNG和JaCoCo进行代码覆盖率测试。首先,详细列出了在pom.xml中配置testNG、reportNG和JaCoCo的依赖以及maven插件的步骤。接着,通过创建maven工程并编写testNG.xml文件,展示了如何设置工程目录和编写测试类。最后,讲解了如何执行测试命令生成JaCoCo报告,并指出了查看报告的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、maven pom配置

testNG依赖

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.11</version>
    <scope>test</scope>
</dependency>

reportNG 依赖

<dependency>
   <groupId>com.google.inject</groupId>
   <artifactId>guice</artifactId>
   <version>3.0</version>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>org.uncommons</groupId>
   <artifactId>reportng</artifactId>
   <version>1.1.4</version>
   <scope>test</scope>
   <exclusions>
       <exclusion>
           <groupId>org.testng</groupId>
           <artifactId>testng</artifactId>
       </exclusion>
   </exclusions>
</dependency>

com.google.inject 这个也要依赖上,否则会报错

Jacoco 依赖

<plugin>
   <groupId>org.jacoco</groupId>
   <artifactId>jacoco-maven-plugin</artifactId>
   <version>0.8.1</version>
   <executions>
       <execution>
           <goals>
               <goal>prepare-agent</goal>
           </goals>
       </execution>
       <execution>
           <id>report</id>
           <phase>test</phase> // 测试期间执行插件
           <goals>
               <goal>report</goal>
           </goals>
       </execution>
   </executions>
</plugin>

maven 插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.1</version>
    <configuration>
        <testFailureIgnore>true</testFailureIgnore>
        <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
        </suiteXmlFiles>
        <!-- 加入编码设置,否则生成的报告会中文乱码 -->
        <argLine> @{
   
   a
### 集成Allure、MavenTestNG于Java项目的指南 #### 安装依赖项 为了在Java项目中集成Allure、Maven以及TestNG,需先配置`pom.xml`文件来引入必要的库。这包括指定版本号并添加相应的依赖声明[^1]。 ```xml <dependencies> <!-- TestNG --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.4.0</version> <scope>test</scope> </dependency> <!-- Allure TestNG Adapter --> <dependency> <groupId>io.qameta.allure</groupId> <artifactId>allure-testng</artifactId> <version>2.13.8</version> <scope>test</scope> </dependency> <!-- Other dependencies as needed... --> </dependencies> ``` #### 插件设置 除了上述依赖外,在同一`pom.xml`文档里还需定义用于生成报告的插件部分。具体来说就是通过增加如下所示的一段XML代码片段完成操作: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <suiteXmlFiles> <suiteXmlFile>${basedir}/src/test/resources/testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> <plugin> <groupId>io.qameta.allure</groupId> <artifactId>allure-maven</artifactId> <version>2.10.0</version> </plugin> </plugins> </build> ``` #### 测试类编写 创建测试案例时应继承自`BaseTest`或者直接使用@Test标注方法,并确保每一个用例都调用了适当的方法来进行断言验证逻辑处理过程中的行为是否符合预期[^2]. ```java import org.testng.annotations.Test; import static io.qameta.allure.Allure.step; public class ExampleTests { @Test(description="An example test case with allure steps") public void testCaseExample(){ step("Step One", () -> { // perform some action here... }); step("Step Two", ()->{ // another set of actions or assertions. }); } } ``` #### 执行命令 最后一步是在终端执行特定指令以运行所有的单元/集成测试并将结果保存到本地磁盘上供后续查看分析之用。通常情况下会采用mvn clean test这样的组合形式来触发整个流程;如果想要立即打开HTML格式的结果页面,则可以在其后面追加-allure-report参数[^3]. ```bash mvn clean test mvn allure:report ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值