用 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

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

相关推荐
SoleMotive.16 小时前
redis实现漏桶算法--https://blog.csdn.net/m0_74908430/article/details/155076710
redis·算法·junit
虹科网络安全2 天前
艾体宝干货 | Redis Python 开发系列#4 保证原子性与性能
redis·python·junit
我发在否2 天前
OpenResty > 平滑升级:1.25.x → 1.27.x
junit·openresty
IMPYLH2 天前
Lua 的 pairs 函数
开发语言·笔记·后端·junit·单元测试·lua
安冬的码畜日常2 天前
【JUnit实战3_35】第二十二章:用 JUnit 5 实现测试金字塔策略
测试工具·junit·单元测试·集成测试·系统测试·bdd·测试金字塔
IMPYLH5 天前
Lua 的 collectgarbage 函数
开发语言·笔记·junit·单元测试·lua
IMPYLH6 天前
Lua 的 assert 函数
开发语言·笔记·junit·单元测试·lua
倚肆8 天前
Spring Boot 日志系统全面详解
spring boot·junit·单元测试
百***48939 天前
Nginx实现接口复制
运维·nginx·junit
安冬的码畜日常11 天前
【JUnit实战3_33】第二十章:用 JUnit 5 进行测试驱动开发(TDD)(下)——TDD 项目的重构过程及新功能的开发实战
测试工具·junit·单元测试·测试驱动开发·tdd·junit5·test-driven