Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Espresso not working with Multidex gives "No tests found"

Tags:

My Espresso tests were running until I had to support multidex.

My build.gradle, I have

minSdkVersion 14
targetSdkVersion 23
multiDexEnabled = true

testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"


androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'

dexOptions {
        jumboMode true
        javaMaxHeapSize "4g"
        incremental true
    }

Test1AuthenticationEspressoTest

@RunWith(AndroidJUnit4.class)
@SmallTest
public class Test1AuthenticationEspressoTest {
    @Rule
    public ActivityTestRule<WelcomeActivity> mActivityRule = new  ActivityTestRule(WelcomeActivity.class);

}

Here is the Error I get

junit.framework.AssertionFailedError: No tests found in com.livestrong.tracker.test.Test1AuthenticationEspressoTest

Any help will be appreciated. Any one has espresso working with multidex ?

like image 833
Sayooj Valsan Avatar asked Jan 07 '16 00:01

Sayooj Valsan


1 Answers

I was having this same problem and it turns out you need to build a custom runner that enables MultiDex and extends from the AndroidJUnitRunner. You then need to set that runner as your testInstrumentationRunner in the build.gradle, and as your runner in your run configuration. There is no need to modify the test class (keep the @RunWith(AndroidJunit4.class)).

Here's a step-by-step of what to do:

  1. Create a class for your custom runner:

    package com.bla.bla.bla;  // your package
    
    import android.os.Bundle;
    import android.support.multidex.MultiDex;
    import android.support.test.runner.AndroidJUnitRunner;
    
    public class CustomTestRunner extends AndroidJUnitRunner
    {
        @Override
        public void onCreate(Bundle arguments)
        {
            MultiDex.install(getTargetContext());
            super.onCreate(arguments);
        }
    }
    
  2. In your build.gradle, set the runner to your custom runner:

    android {
        // ...
        defaultConfig {
            // ...
            testInstrumentationRunner "com.bla.bla.bla.CustomTestRunner"
        }
    }
    
  3. In your run configuration, make sure the instrumentation runner is also set to the same runner.. Note: This step should not be required on Android Studio 3.x and maybe also some previous versions. This option does not exist anymore.

Using the above, I was able to run Espresso tests on our multi-dex enabled app.

I should note that many other posts on the net regarding this topic, suggest setting your runner to com.android.test.runner.MultiDexTestRunner and exculde some dependencies in com.android.support:multidex-instrumentation:1.0.1 in your build.gradle. That solution appears to no longer be the case and doesn't work as of gradle 1.5.0. If you have any of that stuff set, then it'll prevent the above from working. See the comments in this stack overflow post for more information.

like image 173
Billy Avatar answered Oct 12 '22 20:10

Billy