zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

【错误记录】Android Studio 中生成测试覆盖率报告出错 ( ExampleInstrumentedTest > useAppContext[Pixel 2 - 9] FAILED )

Android错误StudioFailed测试 生成 记录 报告
2023-09-14 09:07:28 时间





一、报错信息



在 Android Studio 工程中 , 启用了 " android # buildTypes # debug " 中的 testCoverageEnabled 配置 , 设置为 true , 目的是为了生成测试覆盖率报告 ;

kim.hsl.svg.ExampleInstrumentedTest > useAppContext[Pixel 2 - 9] FAILED 
        org.junit.ComparisonFailure: expected:<kim.hsl.svg[]> but was:<kim.hsl.svg[.tom.jerry]>
        at org.junit.Assert.assertEquals(Assert.java:115)

> Task :app:connectedDebugAndroidTest FAILED

FAILURE: Build failed with an exception.

在这里插入图片描述

build.gradle 配置文件如下 :

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {

    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "kim.hsl.svg"
        minSdkVersion 18
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        // 生成 PNG 图片配置
        //generatedDensities = ['hdpi', 'mdpi', 'xhdpi',  'xxhdpi', 'xxxhdpi']

        // 使用 com.android.support:appcompat 支持库配置
        vectorDrawables.useSupportLibrary = true

        // 国际化资源配置, 只打包默认资源与英文资源
        resConfigs 'en'

        ndk {
            abiFilters "armeabi-v7a" , "arm64-v8a", "x86", "x86_64"
        }

        buildConfigField("boolean", "isGooglePlay", "true")
        buildConfigField("String", "market", '"GooglePlay"')

        applicationIdSuffix ".tom"
    }

    signingConfigs {
        mySigningConfig {
            storeFile file("debug.keystore")
            storePassword "android"
            keyAlias "androiddebugkey"
            keyPassword "android"
        }
    }

    buildTypes {
        release {
            // 是否开启优化混淆
            minifyEnabled true
            // 是否启用资源压缩 , 未使用的资源会被优化
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            applicationIdSuffix ".jerry"
            signingConfig signingConfigs.mySigningConfig
            testCoverageEnabled true
        }
    }
    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.3.2'

    // 矢量图支持库 , 支持 5.0 以下版本手机使用矢量图 , 这个是创建应用时自带的配置
    implementation 'androidx.appcompat:appcompat:1.2.0'

    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}




二、解决方案



分析错误提示 :

kim.hsl.svg.ExampleInstrumentedTest > useAppContext[Pixel 2 - 9] FAILED 
        org.junit.ComparisonFailure: expected:<kim.hsl.svg[]> but was:<kim.hsl.svg[.tom.jerry]>
        at org.junit.Assert.assertEquals(Assert.java:115)

期望得到包名 kim.hsl.svg[] , 但是目前包名为 kim.hsl.svg[.tom.jerry] ,

当前在 " android # defaultConfig " 中设置了 applicationIdSuffix ".tom" 包名后缀 ,

在 " android # buildTypes # debug " 中设置了 applicationIdSuffix ".jerry" 包名后缀 ,

导致最终生成 测试覆盖率报告 出现问题 ;

屏蔽这两个后缀即可正确生成 " 测试覆盖率报告 " ;

再次执行

gradlew :app:createDebugCoverageReport

命令 , 生成 " 测试覆盖率报告 " 成功 ,

在这里插入图片描述

生成路径为 " app\build\reports\coverage\debug " ;

在这里插入图片描述

打开 " app\build\reports\coverage\debug\index.html " 页面 , 内容如下 :

在这里插入图片描述