Отдельные инструментальные тесты добавляются в конфигурации JUnit в Android Studio - PullRequest
2 голосов
/ 19 сентября 2019

Недавно я не смог запустить отдельные инструментальные тесты в Android Studio.Нажатие зеленой стрелки на боковой панели приводит к ошибке: java.lang.TypeNotPresentException: Type androidx.test.ext.junit.runners.AndroidJUnit4 not present, и Studio сообщает, что тесты не найдены.Однако Studio также автоматически добавляет тест как конфигурацию JUnit вместо инструментальных тестов.Тесты расположены соответственно в app/src/androidTest/package/Test.kt.Нажатие стрелки на уровне класса вместо уровня метода работает, но понятно, что раздражает.

Такое ощущение, что это могло быть результатом обновления до Android Studio 3.5.Как я могу вернуть эту функциональность обратно?Соответствующие фрагменты моей сборки Gradle:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

apply plugin: "androidx.navigation.safeargs.kotlin"

apply plugin: "org.jlleitschuh.gradle.ktlint"

android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "package.id"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        testInstrumentationRunnerArguments clearPackageData: 'true'

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    testOptions {
        execution 'ANDROIDX_TEST_ORCHESTRATOR'
    }
}

androidExtensions {
    experimental = true
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    // Koin
    def koin_version = '2.0.1'
    implementation "org.koin:koin-android:$koin_version"
    implementation "org.koin:koin-android-viewmodel:$koin_version"

    // Test libraries
    def androidx_test_version = "2.1.0-rc01"
    testImplementation 'junit:junit:4.12'
    testImplementation "io.mockk:mockk:1.9.3"
    testImplementation "org.koin:koin-test:$koin_version"
    testImplementation "androidx.arch.core:core-testing:$androidx_test_version"
    testImplementation "androidx.room:room-testing:$room_version"
    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
    androidTestUtil 'androidx.test:orchestrator:1.2.0'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation "org.koin:koin-test:$koin_version"
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation "androidx.arch.core:core-testing:$androidx_test_version"

    // Espresso core is included in Barista
    androidTestImplementation('com.schibsted.spain:barista:3.2.0') {
        exclude group: 'com.android.support'
        exclude group: 'org.jetbrains.kotlin' // Only if you already use Kotlin in your project
    }
}

И пример теста будет выглядеть так:

@RunWith(AndroidJUnit4::class)
class FragmentTest : KoinTest {
    @get:Rule val rule = ActivityTestRule(MainActivity::class.java)

    private val dependency by inject<MyObject>()

    @Test
    fun myVeryGoodTest() {
        // Test a thing
    }
}

Я оставил расширение Koin на всякий случай, именно из-за этого возникает проблемахотя, похоже, что его удаление не окажет положительного влияния на проблему.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...