簡體   English   中英

Android Studio 找不到插樁測試

[英]Android Studio can not find instrumented tests

我有一個具有兩種風格版本(專業版和輕型版)的應用程序,我想運行儀器測試。 所以我添加了兩個額外的目錄 androidTestLight 和 androidTestPro(有三個測試文件夾 androidTest、androidTestLight、androidTestPro)。 當我嘗試運行一個簡單的儀器測試時:

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
        assertEquals("com.xxxx.xxxx.pro", appContext.packageName)
    }
}  

我得到這個信息:

12/19 xx:xx:xx : Launching 'ExampleInstrumentedTest' on Samsung Galaxy Tab A7 Lite API 31.
Install successfully finished in 2 s 449 ms.
Running tests

但測試沒有運行,因為 android studio 顯示 0/0 測試。

我使用以下配置:Android Studio Dolphin | 2021.3.1 補丁 1 gradle 7.0.4 compileSdkVersion 31 buildToolVersion 30.0.3 minSdkVersion 29

在 build.gradle 中,我添加了 defaultConfig 部分

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

在我添加的依賴項中

    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    debugImplementation "androidx.fragment:fragment-testing:1.5.5"
    testImplementation "androidx.test:core-ktx:1.5.0"
    testImplementation "org.robolectric:robolectric:4.8"
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
    androidTestImplementation "androidx.test.ext:junit-ktx:1.1.4"
    androidTestImplementation "androidx.test:rules:1.5.0"
    androidTestImplementation "androidx.test:runner:1.5.1"
    androidTestImplementation "androidx.test.uiautomator:uiautomator-v18:2.2.0-alpha1"
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.1.0"

簡單的單元測試運行沒有問題。

你沒有在 defaultConfig 中使用 androidx,試試這個

defaultConfig {
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
        

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM