18

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

Suhas
  • 231
  • 1
  • 3
  • 10

5 Answers5

29

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

Justin Pillay
  • 576
  • 5
  • 9
10

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

enter image description here enter image description here

Ibrahim R Serpici
  • 1,048
  • 7
  • 8
  • 1
    Thank you for this. It gave an insight. Just for anyone that might have same issue, after doing what @Ibrahim suggested, you can now create your package eg. ```com.example.kotlin``` inside the Java folder and all will be well – Codedman Aug 29 '22 at 10:59
  • this works well than the other answers! – Kakaranara Dec 12 '22 at 15:01
  • Thank you very much. I was really bothered, because when creating a kotlin multiplatform mobile project in Android studio, there are no folders created for your ui tests whereas, when you create a normal android project, they are created. So i was really wondering how i could incorporate them . This really helped a lot. – Maverick1st Aug 25 '23 at 08:05
5
  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());
        }
    
    }
    
Artenes Nogueira
  • 1,422
  • 1
  • 14
  • 23
3

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.

Suhas
  • 231
  • 1
  • 3
  • 10
0
  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
Pandorox
  • 19
  • 2