用 Gradle 实现自动化测试:集成 JUnit、TestNG,生成测试报告

我将为你创建一个使用 Gradle 实现自动化测试的配置,集成 JUnit 和 TestNG 并生成测试报告。

首先,我们需要创建一个 build.gradle 文件,这是 Gradle 项目的核心配置文件:

复制代码
plugins {
    // 应用 Java 插件,提供基本的 Java 构建功能
    id 'java'
    // 应用 TestNG 插件
    id 'org.unbroken-dome.testng' version '4.0.0'
}

repositories {
    // 使用 Maven 中央仓库获取依赖
    mavenCentral()
}

dependencies {
    // JUnit 5 依赖
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.2'
    
    // TestNG 依赖
    testImplementation 'org.testng:testng:7.5'
    
    // 可选:添加断言库
    testImplementation 'org.assertj:assertj-core:3.24.2'
}

test {
    // 启用 JUnit 平台
    useJUnitPlatform()
    
    // 配置测试报告
    reports {
        junitXml {
            // 启用 XML 报告
            enabled = true
            // 报告输出目录
            destination = file("$buildDir/test-results/junit")
        }
        html {
            // 启用 HTML 报告
            enabled = true
            // 报告输出目录
            destination = file("$buildDir/reports/tests/junit")
        }
    }
    
    // 测试执行时的日志级别
    testLogging {
        events 'PASSED', 'SKIPPED', 'FAILED'
    }
    
    // 允许并行执行测试
    maxParallelForks = Runtime.runtime.availableProcessors()
}

testNG {
    // 配置 TestNG 测试
    useTestNG()
    
    // 测试报告配置
    reports {
        junitXml {
            enabled = true
            destination = file("$buildDir/test-results/testng")
        }
        html {
            enabled = true
            destination = file("$buildDir/reports/tests/testng")
        }
    }
    
    // TestNG 特定配置
    options {
        // 可以指定测试套件 XML 文件
        // suiteXmlFiles = files('src/test/resources/testng.xml')
        
        // 启用详细日志
        verbose = 2
        
        // 允许并行执行
        parallel = 'methods'
        threadCount = 4
    }
}

// 任务:运行所有测试(JUnit 和 TestNG)
task runAllTests(type: Test) {
    dependsOn test, testNG
    group = 'Verification'
    description = 'Runs all tests (JUnit and TestNG)'
}

// 配置测试源目录
sourceSets {
    test {
        java {
            srcDirs = ['src/test/java']
        }
        resources {
            srcDirs = ['src/test/resources']
        }
    }
}

为了更好地组织测试,我们可以创建一个 TestNG 测试套件配置文件:

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="All Tests Suite" parallel="methods" thread-count="4">
    <test name="TestNG Tests">
        <packages>
            <package name="com.example.testng"/>
        </packages>
    </test>
</suite>

项目结构说明

建议的项目结构如下:

复制代码
project-root/
├── build.gradle
├── src/
│   ├── main/
│   │   └── java/
│   │       └── com/
│   │           └── example/
│   │               └── 你的业务代码
│   └── test/
│       ├── java/
│       │   ├── com/
│       │   │   └── example/
│       │   │       ├── junit/  # JUnit 测试类
│       │   │       └── testng/ # TestNG 测试类
│       └── resources/
│           └── testng.xml     # TestNG 配置文件

使用方法

  1. 将上述配置文件添加到你的项目中
  2. 在相应的目录下编写 JUnit 和 TestNG 测试用例
  3. 运行以下 Gradle 命令:
  • 运行所有测试(JUnit 和 TestNG):./gradlew runAllTests
  • 只运行 JUnit 测试:./gradlew test
  • 只运行 TestNG 测试:./gradlew testNG
  • 生成测试报告:测试执行后自动生成,无需额外命令

测试报告位置

  • JUnit 测试报告:build/reports/tests/junit/index.html
  • TestNG 测试报告:build/reports/tests/testng/index.html

这些报告包含了详细的测试结果,包括通过的测试、失败的测试、跳过的测试以及执行时间等信息,方便你分析测试结果。

相关推荐
__XYZ21 小时前
RedisTemplate 实现分布式锁
java·spring boot·redis·分布式·junit
hweiyu001 天前
Spring Boot 项目集成 Gradle:构建、测试、打包全流程教程
java·spring boot·后端·gradle
阿杰真不会敲代码3 天前
junit单元测试
junit·单元测试
步行cgn5 天前
JUnit 单元测试详细使用指南
junit·sqlserver·单元测试
Knight_AL5 天前
Java 单元测试全攻略:JUnit 生命周期、覆盖率提升、自动化框架与 Mock 技术
java·junit·单元测试
yunmi_6 天前
安全框架 SpringSecurity 入门(超详细,IDEA2024)
java·spring boot·spring·junit·maven·mybatis·spring security
许长安7 天前
Redis(二)——Redis协议与异步方式
数据库·redis·junit
TeleostNaCl9 天前
如何在 IDEA 中使用 Proguard 自动混淆 Gradle 编译的Java 项目
android·java·经验分享·kotlin·gradle·intellij-idea
小白银子14 天前
零基础从头教学Linux(Day 45)
linux·运维·junit·openresty