際際滷

際際滷Share a Scribd company logo
5 Easy Steps to Learn Espresso
Step1. Pre RequisitesforSetup
1. AndroidStudioshouldbe installed
2. Create a EmulatorinAndroidStudioorConnectreal device toruna TestScript.
3. Sample Appshouldbe importedorcode writtenfora DemoProject& can run app successfully.
(If youare not goodat androidbasicsin developmentthenrecommended touse demoprojectapp
of AndroidStudio)
4. Turn of DeveloperSettings ->Turn Off all animations indevice.
Step2: Crate Test Package under targetedProject Directory/App
Go to navigate optioninAndroidStudioandClickonTestoptiontocreate a TestClass.
(UnderApp -> Java -> AndroidTestPackage)
Step3: Sample TestCase Program
OpenyourTestCaseTest.javafileandwrite code asbelow:
//RequiredPackagesforEspresso
importstaticandroid.support.test.espresso.Espresso.onView;
importstaticandroid.support.test.espresso.matcher.ViewMatchers.withText;
importstaticandroid.support.test.espresso.matcher.RootMatchers.withDecorView;
importstaticandroid.support.test.espresso.assertion.ViewAssertions.matches;
@RunWith(AndroidJUnit4.class)
publicclassDemoClassTest{
@Rule
ActivityTestRule<AppActivityClassName>ref var=
new ActivityTestRule<>(AppActivityClass.class);
/**
Ref Code onlyandwrite as per yourapp UI attribute
**/
@Test
publicvoidtestcase1(){
ViewInteractionref =onView(withID(R.id.refID1));
ref.perform(click());
onView(withId(R.id.resultText))
.check(matches(withText(("AsserstionTesttoMatch fromUI"))));
//OR
onView(withID(R.id.refID1)).perform(Click());
onView(withID(R.id.refID3)).perform(typeText("InputText"));
onView(withID(R.id.refID4)).perform(typeText("Input Text2"),closeSoftKeyboard());
onView(withId(R.id.resultText))
.check(matches(withText(("AsserstionTesttoMatch fromUI"))));
}
}
Step4:
Right clickonTest methodandClickon "Run<testMethod>"inEmulatoror Real Device.
Step5: [Optional] Creationof TestSuite formultiple classes
To Create TestSuite formultiple Testclasses(xxTest.java) filesunderandroidTestpackage
Create a class undersame androidTestpackage withclassname as"UnitTestSuite".
importcom.example.android.testing.mysample.DemoClassTest;
importcom.example.android.testing.mysample.DemoClassTest;
importorg.junit.runner.RunWith;
importorg.junit.runners.Suite;
// Runsall unittests.
@RunWith(Suite.class)
@Suite.SuiteClasses({DemoClassTest.class,
CalculatorAddParameterizedTest.class})
publicclassUnitTestSuite {}
AndRun yourTest Suite fromthisclass.Welcome toEspresso!!
Ref Note:
Gradle Info:
applyplugin:'com.android.application'
android{
compileSdkVersion25
buildToolsVersion"25.0.2"
defaultConfig{
applicationId"com.example.ui.espresso"
minSdkVersion15
targetSdkVersion25
versionCode 1
versionName "1.0"
//Addedinstrumentationrunnerforconfiguration-----------------------------------
testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes{
release {
minifyEnabledfalse
proguardFilesgetDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
}
dependencies{
compile fileTree(dir:'libs',include:['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',{
exclude group:'com.android.support',module:'support-annotations'
})
// Espressodependencies----------------------------
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2'){
exclude group:'com.android.support',module:'support-annotations'
}
androidTestCompile('com.android.support.test:runner:0.5'){
exclude group:'com.android.support',module:'support-annotations'
}
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.3.1'
testCompile'junit:junit:4.12'
}
Ref URL:
https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html#run
You can alsouser EspressoRecorderinAndroidStudiotowrite the testcases.
----- CommentsandSuggestionsare welcome.Love totake yourquestions!Thanks ----

More Related Content

5 Easy Steps to Learn Espresso

  • 1. 5 Easy Steps to Learn Espresso Step1. Pre RequisitesforSetup 1. AndroidStudioshouldbe installed 2. Create a EmulatorinAndroidStudioorConnectreal device toruna TestScript. 3. Sample Appshouldbe importedorcode writtenfora DemoProject& can run app successfully. (If youare not goodat androidbasicsin developmentthenrecommended touse demoprojectapp of AndroidStudio) 4. Turn of DeveloperSettings ->Turn Off all animations indevice. Step2: Crate Test Package under targetedProject Directory/App Go to navigate optioninAndroidStudioandClickonTestoptiontocreate a TestClass. (UnderApp -> Java -> AndroidTestPackage) Step3: Sample TestCase Program OpenyourTestCaseTest.javafileandwrite code asbelow: //RequiredPackagesforEspresso importstaticandroid.support.test.espresso.Espresso.onView; importstaticandroid.support.test.espresso.matcher.ViewMatchers.withText; importstaticandroid.support.test.espresso.matcher.RootMatchers.withDecorView; importstaticandroid.support.test.espresso.assertion.ViewAssertions.matches; @RunWith(AndroidJUnit4.class) publicclassDemoClassTest{ @Rule ActivityTestRule<AppActivityClassName>ref var= new ActivityTestRule<>(AppActivityClass.class); /** Ref Code onlyandwrite as per yourapp UI attribute **/ @Test publicvoidtestcase1(){ ViewInteractionref =onView(withID(R.id.refID1)); ref.perform(click()); onView(withId(R.id.resultText)) .check(matches(withText(("AsserstionTesttoMatch fromUI")))); //OR
  • 2. onView(withID(R.id.refID1)).perform(Click()); onView(withID(R.id.refID3)).perform(typeText("InputText")); onView(withID(R.id.refID4)).perform(typeText("Input Text2"),closeSoftKeyboard()); onView(withId(R.id.resultText)) .check(matches(withText(("AsserstionTesttoMatch fromUI")))); } } Step4: Right clickonTest methodandClickon "Run<testMethod>"inEmulatoror Real Device. Step5: [Optional] Creationof TestSuite formultiple classes To Create TestSuite formultiple Testclasses(xxTest.java) filesunderandroidTestpackage Create a class undersame androidTestpackage withclassname as"UnitTestSuite". importcom.example.android.testing.mysample.DemoClassTest; importcom.example.android.testing.mysample.DemoClassTest; importorg.junit.runner.RunWith; importorg.junit.runners.Suite; // Runsall unittests. @RunWith(Suite.class) @Suite.SuiteClasses({DemoClassTest.class, CalculatorAddParameterizedTest.class}) publicclassUnitTestSuite {} AndRun yourTest Suite fromthisclass.Welcome toEspresso!! Ref Note: Gradle Info: applyplugin:'com.android.application' android{ compileSdkVersion25 buildToolsVersion"25.0.2" defaultConfig{ applicationId"com.example.ui.espresso" minSdkVersion15 targetSdkVersion25 versionCode 1
  • 3. versionName "1.0" //Addedinstrumentationrunnerforconfiguration----------------------------------- testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner" } buildTypes{ release { minifyEnabledfalse proguardFilesgetDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro' } } } dependencies{ compile fileTree(dir:'libs',include:['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',{ exclude group:'com.android.support',module:'support-annotations' }) // Espressodependencies---------------------------- androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2'){ exclude group:'com.android.support',module:'support-annotations' } androidTestCompile('com.android.support.test:runner:0.5'){ exclude group:'com.android.support',module:'support-annotations' } compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.android.support:design:25.3.1' testCompile'junit:junit:4.12' } Ref URL: https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html#run You can alsouser EspressoRecorderinAndroidStudiotowrite the testcases. ----- CommentsandSuggestionsare welcome.Love totake yourquestions!Thanks ----