首页 > 解决方案 > 测试用例没有运行 | 自定义 testInstrumentationRunner 不起作用.....?

问题描述

项目

buildscript {
    ext {
        kotlin_version = "1.5.31"
        hilt_version = "2.38.1"
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

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

(:应用程序)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.priyanshumaurya8868.hilttesting"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "com.priyanshumaurya8868.hilttesting.HiltTestRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
    androidTestImplementation "com.google.dagger:hilt-android-testing:$hilt_version"
    kaptAndroidTest "com.google.dagger:hilt-android-compiler:$hilt_version"
    androidTestImplementation "com.google.truth:truth:1.1.3"
}

内部(andorid 测试) TestRunner

package com.priyanshumaurya8868.hilttesting

import android.app.Application
import android.content.Context
import androidx.test.runner.AndroidJUnitRunner

class HiltTestRunner: AndroidJUnitRunner() {
    override fun newApplication(
        cl: ClassLoader?,
        className: String?,
        context: Context?
    ): Application {
        return super.newApplication(cl, HiltTestRunner::class.java.name, context)
    }
}

模块

package com.priyanshumaurya8868.hilttesting

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

@Module
@InstallIn(SingletonComponent::class)
object TestAppModule  {

    @Provides
    fun provideRandomString()="random testing 1"
}

单元测试

package com.priyanshumaurya8868.hilttesting

import androidx.test.filters.SmallTest
import com.google.common.truth.Truth.assertThat
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import javax.inject.Inject


@SmallTest
@HiltAndroidTest
class TestString  {

    @get:Rule(order = 0)
    val hiltRule = HiltAndroidRule(this)

    @Before
    fun init(){
        hiltRule.inject()
    }

    @Inject
    lateinit var str :String

    @Test
    fun test_empty_string(){
     assertThat(str).isNotEmpty()
    }
}

测试结果

测试结果 (0/0) *10/05 16:13:47:在 HMD Global Nokia 6.1 Plus 上启动“TestString”。应用重启成功,无需重新安装以下 APK:com.priyanshumaurya8868.hilttesting 运行测试

$ adb shell am instrument -w -m -e debug false -e class 'com.priyanshumaurya8868.hilttesting.TestString' com.priyanshumaurya8868.hilttesting.test/com.priyanshumaurya8868.hilttesting.HiltTestRunner 连接到设备 'hmd_global-nokia_6_1_plus 上的进程 26841 -DRGID19032402270'.**

标签: androidandroid-studiokotlinandroid-gradle-plugindagger-hilt

解决方案


推荐阅读