安卓-junit5

2022-07-01  本文已影响0人  Darren老师

前言

一个 Gradle 插件,允许使用Android Gradle Plugin 4.0.0 或更高版本在 Android 环境中执行JUnit 5测试。

如何?

此插件为项目的每个构建变体配置单元测试任务以在 JUnit 平台上运行。此外,它通过附加到android.testOptions.

下载

Kotlin

buildscript {
  dependencies {
    classpath("de.mannodermaus.gradle.plugins:android-junit5:1.8.2.1")
  }
}

Groovy

buildscript {
  dependencies {
    classpath "de.mannodermaus.gradle.plugins:android-junit5:1.8.2.1"
  }
}

设置

Kotlin

plugins {
  id("de.mannodermaus.android-junit5")
}

dependencies {
  // (Required) Writing and executing Unit Tests on the JUnit Platform
  testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")
  testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.2")

  // (Optional) If you need "Parameterized Tests"
  testImplementation("org.junit.jupiter:junit-jupiter-params:5.8.2")

  // (Optional) If you also have JUnit 4-based tests
  testImplementation("junit:junit:4.13.2")
  testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.8.2")
}

Groovy

apply plugin: "de.mannodermaus.android-junit5"

dependencies {
  // (Required) Writing and executing Unit Tests on the JUnit Platform
  testImplementation "org.junit.jupiter:junit-jupiter-api:5.8.2"
  testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.8.2"

  // (Optional) If you need "Parameterized Tests"
  testImplementation "org.junit.jupiter:junit-jupiter-params:5.8.2"

  // (Optional) If you also have JUnit 4-based tests
  testImplementation "junit:junit:4.13.2"
  testRuntimeOnly "org.junit.vintage:junit-vintage-engine:5.8.2"
}

要求

此插件的最新版本需要:

仪器测试支持

有对 Android 仪器测试的实验性支持,这需要一些额外的配置和依赖项。此外,由于 JUnit 5 从头开始构建在 Java 8 之上,因此其仪器测试只能在运行 Android 8.0 (API 26) 或更高版本的设备上运行。旧手机将完全跳过这些测试的执行,将它们标记为“忽略”。

要开始使用 JUnit Jupiter 编写仪器测试,请对模块的构建脚本进行以下更改:

Kotlin

android {
  defaultConfig {
    // 1) Make sure to use the AndroidJUnitRunner, or a subclass of it. This requires a dependency on androidx.test:runner, too!
    testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    // 2) Connect JUnit 5 to the runner
    testInstrumentationRunnerArguments["runnerBuilder"] = "de.mannodermaus.junit5.AndroidJUnit5Builder"
  }

  // 3) Java 8 is required
  compileOptions {
    setSourceCompatibility(JavaVersion.VERSION_1_8)
    setTargetCompatibility(JavaVersion.VERSION_1_8)
  }
}
dependencies {
  // 4) Jupiter API & Test Runner, if you don't have it already
  androidTestImplementation("androidx.test:runner:1.4.0")
  androidTestImplementation("org.junit.jupiter:junit-jupiter-api:5.8.2")

  // 5) The instrumentation test companion libraries
  androidTestImplementation("de.mannodermaus.junit5:android-test-core:1.3.0")
  androidTestRuntimeOnly("de.mannodermaus.junit5:android-test-runner:1.3.0")
}

Groovy

android {
  defaultConfig {
    // 1) Make sure to use the AndroidJUnitRunner, or a subclass of it. This requires a dependency on androidx.test:runner, too!
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    // 2) Connect JUnit 5 to the runner
    testInstrumentationRunnerArgument "runnerBuilder", "de.mannodermaus.junit5.AndroidJUnit5Builder"
  }

  // 3) Java 8 is required
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

dependencies {
  // 4) Jupiter API & Test Runner, if you don't have it already
  androidTestImplementation "androidx.test:runner:1.4.0"
  androidTestImplementation "org.junit.jupiter:junit-jupiter-api:5.8.2"

  // 5) The instrumentation test companion libraries
  androidTestImplementation "de.mannodermaus.junit5:android-test-core:1.3.0"
  androidTestRuntimeOnly "de.mannodermaus.junit5:android-test-runner:1.3.0"
}

本地建设

该存储库包含多个模块,分为两个子项目。存储库的根目录包含跨子项目共享的构建逻辑,这些逻辑又使用符号链接连接到其父文件夹中的公共构建脚本。

作者:mannodermaus
链接:https://github.com/mannodermaus/android-junit5

上一篇下一篇

猜你喜欢

热点阅读