android - 测试用例没有运行 | 自定义 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'.**
解决方案
推荐阅读
- python-3.x - 我可以删除名称前缀而不污染名称数据吗?
- android - Android Studio:如何将一个项目从 listview1 添加到 listview2 onClick
- ruby-on-rails - ADFS SAML 身份提供者重定向到状态为“响应者”的服务提供者
- autodesk-forge - Autodesk forge 令牌异常
- vba - 从 vba 访问表单获取 android 设备(例如手机、平板电脑...)中文件(例如图片)的路径
- react-native - 在反应本机项目的 gradle 文件夹中找不到包装器/dists
- amazon-web-services - 无法从 EC2 实例连接到负载均衡器
- xml - 在 Powershell 中删除具有重复 XML 元素的节点
- java - 可以声明一个类的 3 种不同的上下文是什么?
- css - CSS:如何剪辑一个 div