Reputation: 241
I'm working on automation test using Espresso.
The folder in which Instrumentation test classes can be written is missing
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
Reputation: 39
Upvotes: 2
Reputation: 1076
Right click to your src folder and add a new directory then click androidTest\java
Upvotes: 11
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".
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.
Upvotes: 33
Reputation: 1552
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.
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'
Put the testInstrumentationRunner setting in your build.gradle file:
android {
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
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
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