android-junit5检测单元测试'junit-jupiter'未能发现测试

时间:2018-02-01 19:08:28

标签: unit-testing junit5 android-junit

我尝试实施检测单元测试。 我使用的是Android Studio 3.0.1。 我使用https://github.com/mannodermaus/android-junit5

我的顶级build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.2.21'

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath "de.mannodermaus.gradle.plugins:android-junit5:1.0.30"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我的应用级build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: "de.mannodermaus.android-junit5"

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.todo.app"
        minSdkVersion 26
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    testOptions {
        junitPlatform {
            jupiterVersion "5.0.3"
            vintageVersion "4.12.2"

            unitTests {
                applyDefaultTestOptions true
            }

            instrumentationTests {
                enabled true
                version "0.1.1"
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.android.support:cardview-v7:26.1.0'
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.3'
    testCompile "org.mockito:mockito-core:2.13.0"

    testImplementation junit5.unitTests()
    testImplementation junit5.parameterized()
    testCompileOnly junit5.unitTestsRuntime()
    androidTestImplementation junit5.instrumentationTests()
}

repositories {
    mavenCentral()
}

apply plugin: 'kotlin-android-extensions'

在src / androidTest / java / com.myapp.app / ExempleInstrumentedTest下我的测试:

package com.todo.app

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class ExampleInstrumentedTest {

    @Test
    fun test() {

        // Read the data.
        assertEquals(true, true, "test")
    } 
} 

我在android studio 3.0.1中启动测试时遇到此错误:

  

FEVR。 01,20188 7:48:50 PM   org.junit.platform.launcher.core.DefaultLauncher handleThrowable   AVERTISSEMENT:ID为'junit-jupiter'的TestEngine未能发现   测试org.junit.platform.commons.util.PreconditionViolationException:   无法加载名称为的类:com.todo.app.ExampleInstrumentedTest

     

FEVR。 01,20188 7:48:50 PM   org.junit.platform.launcher.core.DefaultLauncher handleThrowable   AVERTISSEMENT:ID为'junit-vintage'的TestEngine未能发现   测试org.junit.platform.commons.util.PreconditionViolationException:   无法加载名称为的类:com.todo.app.ExampleInstrumentedTest

有什么问题?

1 个答案:

答案 0 :(得分:-1)

我不知道这对你有什么影响,但在我的情况下,我必须让测试类公开为Android Studio / Intellij测试运行器插件才能真正找到测试。