Search code examples
androidgradlejunitjunit4android-testing

Cannot run androidTest in certain module


I am having a problem running an androidTest. It cannot locate my tests. The weird thing is, that it's only the case for a certain module. Other modules which I put the test into and configure their build.gradle files eqully are working. Here are some informations. Any help is very appreciated!

The output is as follows:

Running tests

$ adb shell am instrument -w -r   -e debug false -e class my.package.MainActivityTest my.package.module.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
Test running failed: Unable to find instrumentation info for: ComponentInfo{my.package.test/android.support.test.runner.AndroidJUnitRunner}
Empty test suite.

The test is located under

myModule/src/androidTest/java/my/package/MainActivityTest.java

and looks as follows:

@LargeTest
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void thisTestIsntExecuted() {
        assertEquals(1, 2);
    }
}

The modules build.gradle file has the following settings:

android {
    defaultConfig {
        (...)
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}
dependencies {
    (...)
    testImplementation "junit:junit:4.12"
    androidTestImplementation "com.android.support.test.espresso:espresso-core:3.0.1"
    androidTestImplementation "com.android.support.test:runner:1.0.1"
}

Instrumentation runner on device:

instrumentation:my.package.test/android.support.test.runner.AndroidJUnitRunner (target=my.package.module)

//edit: I had a closer look at the output from the run command and the instrumentation runner on the device. I forgot to add .module on two places.


Solution

  • I found out to solve the problem. As stated in the comment above I was able to start my tests manually with the command:

    adb shell am instrument -w -r -e debug false -e class my.package.module.MainActivityTest my.package.test/android.support.test.runner.AndroidJUnitRunner 
    

    The run configuration which Android Studio created for me executed the command:

    adb shell am instrument -w -r -e debug false -e class my.package.module.MainActivityTest my.package.module.test/android.support.test.runner.AndroidJUnitRunner
    

    So as you can see a path was wrong, the module part was too much.

    This path is the testApplicationId and can be changed via gradle, see here. When it's not set, the value is automatically created by taking the applicationId and add a .test to it. This resulted in a wrong path in my case, maybe because I am generating the applicationId based on the build variant.

    Don't forget to sync and clean.