Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No tests found when running instrumented tests with AndroidX

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".

like image 592
olegstepanov Avatar asked Dec 03 '18 19:12

olegstepanov


People also ask

What are instrumentation tests?

Note: Instrumented test, also known as instrumentation tests, are initialized in a special environment that gives them access to an instance of Instrumentation. This class provides access to the application context and APIs to manipulate the app under test and gives instrumented tests their name.

What is instrumental test in Android?

Instrumented tests are tests that run on physical devices and emulators, and they can take advantage of the Android framework APIs and supporting APIs, such as AndroidX Test.

How to run Android unit test in command line?

To run a test from the command line, run adb shell to start a command line shell on your device or emulator. Inside that shell you can interact with the activity manager using the am command and use its instrument subcommand to run your tests.

What is AndroidX test core?

AndroidX Test is a collection of Jetpack libraries that lets you run tests against Android apps. It also provides a series of tools to help you write these tests. For example, AndroidX Test provides JUnit4 rules to start activities and interact with them in JUnit4 tests.


1 Answers

I replaced -

testInstrumentationRunner "androidx.test.runner.AndroidJUnit4"

with -

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

like image 127
yash zanwar Avatar answered Nov 07 '22 01:11

yash zanwar