Suhas
Suhas

Reputation: 241

Android studio - androidTest folder is missing

I'm working on automation test using Espresso.

The folder in which Instrumentation test classes can be written is missing

Screenshot of the project

I tried by adding

android{    
        sourceSets{
        main { java.srcDirs = ['src/main/java'] }
        test { java.srcDirs = ['src/test/java'] }
        androidTest { java.srcDirs = ['src/androidTest/java'] }
    }

}

inside build.gradle but didn't work.

Tried to generate by using other solutions provided in this question still doesn't work

Upvotes: 19

Views: 18032

Answers (5)

Pandorox
Pandorox

Reputation: 39

  1. Close the Android Studio
  2. Remove ".idea" folder in project root.
  3. Create "androidTest" folder in Module you want to test.
  4. Re-open Android Studio and open your project

Upvotes: 2

Ibrahim R Serpici
Ibrahim R Serpici

Reputation: 1076

Right click to your src folder and add a new directory then click androidTest\java

enter image description here enter image description here

Upvotes: 11

Justin Pillay
Justin Pillay

Reputation: 626

The quickest way is to auto-generate the folder and record an espresso test. On the top menu bar of Android Studio, click on "Run" then -> "Record Espresso Test".

Run the espresso test recoder

This will start your emulator and also open up the recorder dialog. Then just perform a few actions on your emulator and then click "ok" on the recorder dialog. Android Studio will generate the test as well as the missing folder. You will basically have a template test setup for you to start editing as you wish.

Click OK on recoder dialog

Upvotes: 33

Artenes Nogueira
Artenes Nogueira

Reputation: 1552

  1. Create your androidTest folder like any other folder. Put it under app/src. You can event create this folder outside Android Studio if you want.

  2. Put the right dependencies in your build.gradle file. Something like this:

    testImplementation 'junit:junit:4.12'
    testImplementation 'androidx.arch.core:core-testing:2.0.1'
    
    androidTestImplementation 'androidx.test:rules:1.1.1'
    androidTestImplementation 'androidx.test:runner:1.1.2-alpha02'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.1'
    
  3. Put the testInstrumentationRunner setting in your build.gradle file:

    android {
       defaultConfig {
          testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
       }
    }
    
  4. And in your test class, which you will save under app/src/androidTest/[your namespace here], will look something like this:

    import android.content.Context;
    import android.support.test.InstrumentationRegistry;
    import android.support.test.runner.AndroidJUnit4;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    
    import static org.junit.Assert.assertEquals;
    
    @RunWith(AndroidJUnit4.class)
    public class PrincipalTest {
    
        @Test
        public void testAppContext() {
            Context context = InstrumentationRegistry.getTargetContext();
            assertEquals("my.package.name", context.getPackageName());
        }
    
    }
    

Upvotes: 5

Suhas
Suhas

Reputation: 241

Copy and pasted existing test folder, renamed it as "androidTest" in file system worked for me.

Because my project was shifted to new repository recently, so the folder got deleted.

Upvotes: 3

Related Questions