三个测试容器从哪里来?

时间:2017-12-11 18:05:31

标签: java kotlin junit5

我整理了一个演示JUnit5项目来试用该框架。该项目由Gradle(4.4),Java(8)和Kotlin(1.2.0)组成,包含4个测试用例。我有以下Gradle构建脚本(我删除了大部分样板文件以仅保留重要内容):

apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'org.junit.platform.gradle.plugin'

repositories {
    mavenCentral()
}

buildscript {
    ext.kotlin_version = '1.2.0'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

configurations {
    all {
        exclude group: 'junit', module: 'junit'
    }
}

project.ext {
    junitPlatformVersion = '1.0.2'
    junitJupiterVersion = '5.0.2'
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    testCompile "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}"
    testRuntime "org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}"
}

junitPlatform {
    platformVersion '1.0.2'
    filters {
        engines {
            include 'junit-jupiter'
        }
    }
}

我还有一个KotlinTest.kt和一个JavaTest.java,它们具有相同的测试用例:

@Test
fun junit5TestPasses() {
    assertTrue(true)
}

@Test
fun junit5TestFails() {
    assertTrue(false)
}

当我使用gradlew junitPlatformTest运行测试时,我正确地看到2个测试通过,2个测试失败。但是,我也看到“发现了3个容器”。我的问题是为什么找到3个容器?这些是什么?我似乎无法在JUnit5用户指南中找到与此场景相关的测试容器的直接答案。

1 个答案:

答案 0 :(得分:4)

3个容器= JUnit Jupiter引擎 + dynamic_cast<MoreStuff*>(...) + KotlinTest.class

引擎,JavaTest.class的实现,也被视为容器。下一级是包含TestEngine - 注释方法的类。查看从user-guide复制的示例:

@Test

在这里您可以看到五个容器,即:

  1. JUnit Vintage 引擎
  2. example.JUnit4Tests class
  3. JUnit Jupiter 引擎
  4. StandardTests
  5. 特殊测试用例
  6. 所有六片叶子都是测试。

    要查看为您的测试计划投放的类似tree,请将├─ JUnit Vintage │ └─ example.JUnit4Tests │ └─ standardJUnit4Test ✔ └─ JUnit Jupiter ├─ StandardTests │ ├─ succeedingTest() ✔ │ └─ skippedTest() ↷ for demonstration purposes └─ A special test case ├─ Custom test name containing spaces ✔ ├─ ╯°□°)╯ ✔ └─ ✔ Test run finished after 64 ms [ 5 containers found ] [ 0 containers skipped ] [ 5 containers started ] [ 0 containers aborted ] [ 5 containers successful ] [ 0 containers failed ] [ 6 tests found ] [ 1 tests skipped ] [ 5 tests started ] [ 0 tests aborted ] [ 5 tests successful ] [ 0 tests failed ] 添加到Gradle details 'tree'任务。