No tests found when running instrumented tests wit

2020-04-08 13:57发布

I'm trying to run the standard ExampleInstrumentedTest in my Android project (which uses AndroidX), but get "No tests found" error instead. I've looked through the other questions and the documentation and I'm pretty sure I've done everything right, but maybe I'm overlooking anything?

Here is my app's build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 24
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.ext.junit.runners.AndroidJUnit4"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    // Gradle automatically adds 'android.test.runner' as a dependency.
    useLibrary 'android.test.runner'

    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'
}

repositories {
    mavenCentral()
    maven {
        url("https://oss.sonatype.org/content/repositories/snapshots")
    }
    maven { url 'https://jitpack.io' }
}


dependencies {
    // Core library
    androidTestImplementation 'androidx.test:core:1.0.0'

    // AndroidJUnitRunner and JUnit Rules
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test:rules:1.1.0'

    // Assertions
    androidTestImplementation 'androidx.test.ext:junit:1.0.0'
    androidTestImplementation 'androidx.test.ext:truth:1.0.0'
    androidTestImplementation 'com.google.truth:truth:0.42'

    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}

// More dependencies...    

and ExampleInstrumentedTest.java:

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

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import static org.junit.Assert.*;

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        Context appContext = ApplicationProvider.getApplicationContext();
        assertEquals("XXX", appContext.getPackageName());
    }
}

when I run the code, I get "No tests found".

3条回答
爷、活的狠高调
2楼-- · 2020-04-08 14:03

I needed to replace my runner

"androidx.test.ext.junit.runners.AndroidJUnit4"

with

"androidx.test.runner.AndroidJUnitRunner"
查看更多
神经病院院长
3楼-- · 2020-04-08 14:24

Look for app/src/androidTest/AndroidManifest.xml

change android:name to androidx.multidex.MultiDexApplication

<application
        android:allowBackup="true"
        android:label="Components Tests App"
        android:supportsRtl="true"
        android:name="android.support.multidex.MultiDexApplication">

To

<application
            android:allowBackup="true"
            android:label="Components Tests App"
            android:supportsRtl="true"
            android:name="androidx.multidex.MultiDexApplication">
查看更多
Melony?
4楼-- · 2020-04-08 14:26

I replaced -

testInstrumentationRunner "androidx.test.runner.AndroidJUnit4"

with -

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

查看更多
登录 后发表回答