如何使用Gradle配置JUnit 5

IT 文章2年前 (2023)发布 小编
0 0 0

JUnit 5与Gradle。了解如何配置JUnit 5与Gradle,不同的模块以及如何使用它们来创建和执行测试。

请注意,JUnit 5在运行时需要Java 8。

1.JUnit 5 Gradle依赖项

要通过Gradle运行JUnit 5测试,您应该至少包含以下依赖项。

ad

程序员导航

优网导航旗下整合全网优质开发资源,一站式IT编程学习与工具大全网站

  • junit-jupiter-api: 这是所有核心注释所在的主要模块,例如@Test,生命周期方法注释和断言。
  • junit-jupiter-engine: 它具有测试引擎实现,在运行时执行测试时需要。
  • junit-platform-suite: 此模块提供@Suite支持,使JUnitPlatform运行时代理过时。
  • junit-jupiter-params: 对JUnit 5中的参数化测试提供支持。

build.gradle:

dependencies {
    testRuntime("org.junit.jupiter:junit-jupiter-api:5.8.1")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:5.8.1")
    testRuntime("org.junit.jupiter:junit-jupiter-params:5.8.1")
    testRuntime("org.junit.platform:junit-platform-suite:1.8.1")
}
test {
    useJUnitPlatform()
}

2. 使用JUnit 5执行JUnit 4测试

要在JUnit 5环境中执行JUnit 4测试,您需要包含junit-vintage-engine依赖项。JUnit Vintage为在平台上运行基于JUnit 4的测试提供了一个TestEngine。

dependencies {
    //运行 junit 4 测试
    testCompile("junit:junit:4.12")
    testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M4")
}

通过在build.gradle中配置上述内容,现在您可以使用JUnit 5 Jupiter来运行旧的JUnit 4测试。

3.JUnit 5 Gradle示例

以下是使用junit 5构建测试的build.gradle文件的示例:

ad

AI 工具导航

优网导航旗下AI工具导航,精选全球千款优质 AI 工具集

buildscript {
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
	}
}
repositories {
	mavenCentral()
}
ext.junit4Version        = '4.12'
ext.junitVintageVersion  = '4.12.2'
ext.junitPlatformVersion = '1.8.1'
ext.junitJupiterVersion  = '5.8.1'
ext.log4jVersion         = '2.9.0'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.junit.platform.gradle.plugin'
jar {
	baseName = 'junit5-gradle-consumer'
	version = '1.0.0-SNAPSHOT'
}
compileTestJava {
	sourceCompatibility = 1.8
	targetCompatibility = 1.8
	options.compilerArgs += '-parameters'
}
junitPlatform {
	// platformVersion '1.0.2'
	filters {
		engines {
			// include 'junit-jupiter', 'junit-vintage'
			// exclude 'custom-engine'
		}
		tags {
			// include 'fast'
			exclude 'slow'
		}
		// includeClassNamePattern '.*Test'
	}
	// configurationParameter 'junit.jupiter.conditions.deactivate', '*'
	// enableStandardTestTask true
	// reportsDir file('build/test-results/junit-platform') // this is the default
	logManager 'org.apache.logging.log4j.jul.LogManager'
}
dependencies {
	// JUnit Jupiter API and TestEngine implementation
	testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
	testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")
	// If you also want to support JUnit 3 and JUnit 4 tests
	testCompile("junit:junit:${junit4Version}")
	testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}")
	// To avoid compiler warnings about @API annotations in JUnit code
	testCompileOnly('org.apiguardian:apiguardian-api:1.0.0')
	// To use Log4J's LogManager
	testRuntime("org.apache.logging.log4j:log4j-core:${log4jVersion}")
	testRuntime("org.apache.logging.log4j:log4j-jul:${log4jVersion}")
	// Only needed to run tests in an (IntelliJ) IDE(A) that bundles an older version
	testRuntime("org.junit.platform:junit-platform-launcher:${junitPlatformVersion}")
}
task wrapper(type: Wrapper) {
	description = 'Generates gradlew[.bat] scripts'
	gradleVersion = '4.3.1'
}

 

© 版权声明

相关文章

暂无评论

暂无评论...