Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 726
Suhas Avatar asked Nov 12 '18 13:11

Suhas


People also ask

How to fix “SDK tools Directory missing” in Android Studio?

So to fix “SDK tools directory missing” in android studio, try the following methods The Cause for this error -SDK tools package might not be installed. Quick fix: Go to the Tools –> SDK manager –> SDK tools. Deselect Hide obsolete packages option on the right bottom and further install Android SDK Tools (obsolete).

Where is the appdata folder in Android Studio?

You have installed Android Studio and now looking for Android SDK and other things. AppData folder should be inside your user directory. You are worried that you are not able to see AppData folder inside your user directory. Don't worry, it is hidden. You can easily make it visible.

How to set up instrumentation tests in Android Studio?

Clicking on the tab should open a pane with your modules and build variants for those modules. Above the modules is a Test Artifact dropdown which you should change to Android Instrumentation Tests. Show activity on this post.

How to fix Android SDK not installed?

Quick fix: Uninstall Android SDK (default location C:\Users\..\AppData\Local\Android\Sdk ) and install it again. This time affirms the minimum required storage space at your place of download. After execution check, if tools folder is now added in SDK directory.


3 Answers

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

like image 81
Justin Pillay Avatar answered Jan 04 '23 06:01

Justin Pillay


  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());
        }
    
    }
    
like image 31
Artenes Nogueira Avatar answered Jan 04 '23 06:01

Artenes Nogueira


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

enter image description here enter image description here

like image 34
Ibrahim R Serpici Avatar answered Jan 04 '23 06:01

Ibrahim R Serpici