<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>testing &#8211; Wiebe Elsinga</title>
	<atom:link href="http://wiebe-elsinga.com/blog/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://wiebe-elsinga.com/blog</link>
	<description>Blog</description>
	<lastBuildDate>Thu, 09 Apr 2015 09:21:33 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.8.1</generator>
	<item>
		<title>What’s new in Android Testing</title>
		<link>http://wiebe-elsinga.com/blog/whats-new-in-android-testing/</link>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Thu, 09 Apr 2015 02:20:09 +0000</pubDate>
				<category><![CDATA[AndroidDev Tips]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[espresso]]></category>
		<category><![CDATA[testing]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1755</guid>

					<description><![CDATA[During Droidcon Italy Stephan Linzner from Google explained upcoming features in the Android Testing Support Library. Lets have a closer look at some: Test Filtering If you want to suppress a test to run on certain target Api levels, just add the @SdkSuppress annotation. @SdkSuppress&#40;minSdkVersion=15&#41; @Test public void featureWithMinSdk15&#40;&#41; &#123; &#160; ... &#125; You can also filter tests to only run on a (physical) device by adding the @RequiresDevice annotation. @RequiresDevice @Test public void SomeDeviceSpecificFeature&#40;&#41; &#123; &#160; ... &#125; ActivityTestRule Because ActivityInstrumentationTestCase2 will become deprecated you need to define a @Rule. Rules allow very flexible addition or redefinition of the behavior of each test method in a test class https://github.com/junit-team/junit/wiki/Rules So in your test add a @Rule annotation, and create an ActivityTestRule for your Activity @RunWith&#40;AndroidJUnit4.class&#41; @LargeTest public class ChangeTextBehaviorTest &#123; &#160; &#160; ... &#160; &#160; @Rule &#160; &#160; public ActivityTestRule&#60;MainActivity&#62; mActivityRule = new ActivityTestRule&#60;&#62;&#40;MainActivity.class&#41;; &#160; &#160; @Test &#160; &#160; public void changeText_sameActivity&#40;&#41; &#123; &#160; &#160; &#160; &#160; // Type text and then press the button. &#160; &#160; &#160; &#160; onView&#40;withId&#40;R.id.editTextUserInput&#41;&#41;.perform&#40;typeText&#40;STRING_TO_BE_TYPED&#41;, closeSoftKeyboard&#40;&#41;&#41;; &#160; &#160; &#160; &#160; onView&#40;withId&#40;R.id.changeTextBt&#41;&#41;.perform&#40;click&#40;&#41;&#41;; &#160; &#160; &#160; &#160; // Check that the text was changed. &#160; &#160; &#160; &#160; onView&#40;withId&#40;R.id.textToBeChanged&#41;&#41;.check&#40;matches&#40;withText&#40;STRING_TO_BE_TYPED&#41;&#41;&#41;; &#160; &#160; &#125; &#125; Now create your ActivityTestRule public class ActivityTestRule&#60;T extends Activity&#62; &#123; &#160; &#160; public T getActivity&#40;&#41; &#123;&#125; &#160; &#160; protected Intent getActivityIntent&#40;&#41; &#123;&#125; &#160; &#160; protected void beforeActivityLaunched&#40;&#41; &#123;&#125; &#160; &#160; protected void afterActivityFinished&#40;&#41; &#123;&#125; &#125; Espresso-Intents Espresso-Intents is like Mockito but for Intent, basically hermetic inter-app testing. To give you an example, lets say in your application when a user presses a &#8220;call&#8221; button, you want to know if the correct data will be send to the Intent.ACTION_CALL. So first create an IntentsTestRule and then verify that the Intent was sent using Espresso-Intents @RunWith&#40;AndroidJUnit4.class&#41; @LargeTest public class DialerActivityTest &#123; &#160; &#160; ... &#160; &#160; @Rule &#160; &#160; public IntentsTestRule&#60;DialerActivity&#62; mRule = new IntentsTestRule&#60;&#62;&#40;DialerActivity.class&#41;; &#160; &#160; @Test &#160; &#160; public void typeNumber_ValidInput_InitiatesCall&#40;&#41; &#123; &#160; &#160; &#160; &#160; onView&#40;withId&#40;R.id.edit_text_caller_number&#41;&#41;.perform&#40;typeText&#40;VALID_PHONE_NUMBER&#41;, &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; closeSoftKeyboard&#40;&#41;&#41;; &#160; &#160; &#160; &#160; onView&#40;withId&#40;R.id.button_call_number&#41;&#41;.perform&#40;click&#40;&#41;&#41;; &#160; &#160; &#160; &#160; intended&#40;allOf&#40; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; hasAction&#40;is&#40;equalTo&#40;Intent.ACTION_CALL&#41;&#41;&#41;, &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; hasData&#40;equalTo&#40;INTENT_DATA_PHONE_NUMBER&#41;&#41;, &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; toPackage&#40;PACKAGE_ANDROID_DIALER&#41;&#41;&#41;; &#160; &#160; &#125; &#125; So now you know&#8230; All code can be found in android-testing github: ActivityTestRule Sample IntentsBasicSample General Espresso Github samples by Google Thanks Stephan Linzner for the resources. #HappyTesting]]></description>
										<content:encoded><![CDATA[<p><img loading="lazy" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/DroidconIT.jpg" width="4096" height="3040" class="aligncenter size-full wp-image-1779" data-wp-pid="1779" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/DroidconIT.jpg 4096w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/DroidconIT-300x223.jpg 300w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/DroidconIT-1024x760.jpg 1024w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/DroidconIT-800x594.jpg 800w" sizes="(max-width: 4096px) 100vw, 4096px" /></p>
<p>During Droidcon Italy <a href="https://plus.google.com/+StephanLinzner">Stephan Linzner</a> from Google explained upcoming features in the <a href="https://developer.android.com/tools/support-library/index.html">Android Testing Support Library</a>. Lets have a closer look at some:<br />
<span id="more-1755"></span><br />
</p>
<h3>Test Filtering</h3>
<p>If you want to suppress a test to run on certain target Api levels, just add the <code class="codecolorer bash default"><span class="bash"><span style="color: #000000; font-weight: bold;">@</span>SdkSuppress</span></code> annotation.</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@SdkSuppress<span style="color: #009900;">&#40;</span>minSdkVersion<span style="color: #339933;">=</span><span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><br />
@Test<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> featureWithMinSdk15<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; ...<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>You can also filter tests to only run on a (physical) device by adding the <code class="codecolorer bash default"><span class="bash"><span style="color: #000000; font-weight: bold;">@</span>RequiresDevice</span></code> annotation.</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@RequiresDevice<br />
@Test<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> SomeDeviceSpecificFeature<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; ...<br />
<span style="color: #009900;">&#125;</span></div></div>
<p></p>
<h3>ActivityTestRule</h3>
<p>Because <code class="codecolorer bash default"><span class="bash">ActivityInstrumentationTestCase2</span></code> will become deprecated you need to define a <code class="codecolorer bash default"><span class="bash"><span style="color: #000000; font-weight: bold;">@</span>Rule.</span></code></p>
<blockquote><p>Rules allow very flexible addition or redefinition of the behavior of each test method in a test class</p></blockquote>
<div align="right" ><small>https://github.com/junit-team/junit/wiki/Rules</small></div>
<p>
So in your test add a <code class="codecolorer bash default"><span class="bash"><span style="color: #000000; font-weight: bold;">@</span>Rule</span></code> annotation, and create an <code class="codecolorer bash default"><span class="bash">ActivityTestRule</span></code> for your Activity</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@RunWith<span style="color: #009900;">&#40;</span>AndroidJUnit4.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
@LargeTest<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ChangeTextBehaviorTest <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; ...<br />
<br />
&nbsp; &nbsp; @Rule<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> ActivityTestRule<span style="color: #339933;">&lt;</span>MainActivity<span style="color: #339933;">&gt;</span> mActivityRule <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ActivityTestRule<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span>MainActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> changeText_sameActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Type text and then press the button.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">editTextUserInput</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>typeText<span style="color: #009900;">&#40;</span>STRING_TO_BE_TYPED<span style="color: #009900;">&#41;</span>, closeSoftKeyboard<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">changeTextBt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Check that the text was changed.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">textToBeChanged</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">check</span><span style="color: #009900;">&#40;</span>matches<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span>STRING_TO_BE_TYPED<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Now create your <code class="codecolorer bash default"><span class="bash">ActivityTestRule</span></code></p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ActivityTestRule<span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> Activity<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> T getActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> Intent getActivityIntent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> beforeActivityLaunched<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> afterActivityFinished<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p></p>
<h3>Espresso-Intents</h3>
<p>Espresso-Intents is like <a href="http://site.mockito.org/">Mockito</a> but for Intent, basically hermetic inter-app testing.<br />
To give you an example, lets say in your application when a user presses a &#8220;call&#8221; button, you want to know if the correct data will be send to the <code class="codecolorer bash default"><span class="bash">Intent.ACTION_CALL</span></code>. </p>
<p><img loading="lazy" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/EspressoIntent.png" alt="EspressoIntent" width="1067" height="573" class="aligncenter size-full wp-image-1768" data-wp-pid="1768" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/EspressoIntent.png 1067w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/EspressoIntent-300x161.png 300w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/EspressoIntent-1024x550.png 1024w, http://wiebe-elsinga.com/blog/wp-content/uploads/2015/04/EspressoIntent-800x430.png 800w" sizes="(max-width: 1067px) 100vw, 1067px" /></p>
<p>So first create an <code class="codecolorer bash default"><span class="bash">IntentsTestRule</span></code> and then verify that the Intent was sent using Espresso-Intents</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@RunWith<span style="color: #009900;">&#40;</span>AndroidJUnit4.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
@LargeTest<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DialerActivityTest <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; ...<br />
&nbsp; &nbsp; @Rule<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> IntentsTestRule<span style="color: #339933;">&lt;</span>DialerActivity<span style="color: #339933;">&gt;</span> mRule <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> IntentsTestRule<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span>DialerActivity.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Test<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> typeNumber_ValidInput_InitiatesCall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">edit_text_caller_number</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>typeText<span style="color: #009900;">&#40;</span>VALID_PHONE_NUMBER<span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; closeSoftKeyboard<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">button_call_number</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; intended<span style="color: #009900;">&#40;</span>allOf<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasAction<span style="color: #009900;">&#40;</span>is<span style="color: #009900;">&#40;</span>equalTo<span style="color: #009900;">&#40;</span>Intent.<span style="color: #006633;">ACTION_CALL</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasData<span style="color: #009900;">&#40;</span>equalTo<span style="color: #009900;">&#40;</span>INTENT_DATA_PHONE_NUMBER<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toPackage<span style="color: #009900;">&#40;</span>PACKAGE_ANDROID_DIALER<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p></br><br />
So now you know&#8230;<br />
<br />
All code can be found in <a href="https://github.com/googlesamples/android-testing">android-testing github</a>:</p>
<ul>
<li><a href="https://github.com/googlesamples/android-testing/tree/master/espresso/BasicSample">ActivityTestRule Sample</a></li>
<li><a href="https://github.com/googlesamples/android-testing/tree/master/espresso/IntentsBasicSample">IntentsBasicSample</a></li>
<li><a href="">General Espresso Github samples by Google</a></li>
</ul>
<p>
Thanks <a href="https://plus.google.com/+StephanLinzner">Stephan Linzner</a> for the resources. #HappyTesting</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>The Hitchhiker&#8217;s guide to Android Testing, part 2: Espresso</title>
		<link>http://wiebe-elsinga.com/blog/the-hitchhikers-guide-to-android-testing-part-2-espresso/</link>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Fri, 03 Apr 2015 02:04:02 +0000</pubDate>
				<category><![CDATA[AndroidDev Tips]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[AndroidDev]]></category>
		<category><![CDATA[espresso]]></category>
		<category><![CDATA[testing]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1711</guid>

					<description><![CDATA[As mentioned in the first article Ali Derbane and me wrote about Android Functional Testing, there are a lot of frameworks you can use. In this second part of the journey I will be explaining the functional testing framework called Espresso. So now you know a bit more about Espresso. If you want to know more, you may want to visit: Espresso website Github repo corresponding to this article General Espresso Github samples by Google Updated (08-04) When running the tests with gradle you can just use gradle connectedAndroidTest]]></description>
										<content:encoded><![CDATA[<p>As mentioned in the <a href="http://www.technotalkative.com/part-10-the-hitchhikers-guide-to-android-testing/">first article</a> <a href="https://plus.google.com/+AliDerbane">Ali Derbane</a> and me wrote about Android Functional Testing, there are a lot of frameworks you can use. In this second part of the journey I will be explaining the functional testing framework called <a href="https://code.google.com/p/android-test-kit/">Espresso</a>.<br />
<span id="more-1711"></span><br />
<img src="http://i.giphy.com/VtFd68Pr19fYk.gif" width=100%" /></p>
<h3>Introduction</h3>
<p>Introduced at the GTAC in 2013, Espresso is designed to be used in environments where the developers write their own tests, and makes it possible to write concise, beautiful, and reliable Android UI tests quickly.<br />
Espresso has several general components:</p>
<ul>
<li>The <code class="codecolorer bash default"><span class="bash">Espresso</span></code> class offers the <code class="codecolorer bash default"><span class="bash">onView</span></code> and <code class="codecolorer bash default"><span class="bash">onData</span></code> methods which, alone, can be used for a good numbers of possible tests on a given interface.</li>
<li><code class="codecolorer bash default"><span class="bash">ViewMatchers</span></code> contains a collection of objects that implements the interface <code class="codecolorer bash default"><span class="bash">Matcher <span style="color: #000000; font-weight: bold;">&lt;</span>? super View<span style="color: #000000; font-weight: bold;">&gt;</span></span></code>. Using this Class you can collect and check View elements. For example, getting a View element (Button) with text &#8220;7&#8221;.</li>
<li><code class="codecolorer bash default"><span class="bash">ViewActions</span></code> contains a collection of <code class="codecolorer bash default"><span class="bash">viewAction</span></code> objects to perform actions on a view. These actions are passed to the method <code class="codecolorer bash default"><span class="bash">ViewInteraction.perform</span></code> and may contain multiple actions. For example, clicking on a View element (Button).</li>
<li><code class="codecolorer bash default"><span class="bash">ViewAssertions</span></code> contains a collection of <code class="codecolorer bash default"><span class="bash">ViewAssertion</span></code> to conduct checks on views. </li>
</ul>
<p>
To illustrate these components a test can look like this:</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&nbsp; Espresso.<span style="color: #006633;">onView</span><span style="color: #009900;">&#40;</span>ViewMatchers.<span style="color: #006633;">withText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;7&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>ViewActions.<span style="color: #006633;">click</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; Espresso.<span style="color: #006633;">onView</span><span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">check</span><span style="color: #009900;">&#40;</span>ViewAssertions.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span>ViewMatchers.<span style="color: #006633;">withText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;42&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>And the good news, as of last year Google has introduced a <a href="https://developer.android.com/tools/support-library/index.html">Testing Support Library</a> containing Espresso. So lets start by implementing Espresso.</p>
<div style="background: lightgray; padding: 12pt;">To illustrate, we are going to write some tests that tests agains a <a href="https://github.com/welsinga/sample_espresso/app">Android calculator application</a>. The common test scenario we will be implementing is testing if <em>‘6’ x ‘7’ equals ‘42’</em>.</div>
<p>&nbsp;&nbsp;</p>
<h3>Define the test runner</h3>
<p>To use Espresso we first need to define who is running the tests. Espresso uses a new runner named <code class="codecolorer bash default"><span class="bash">AndroidJUnitRunner</span></code>. This runner, based on <code class="codecolorer bash default"><span class="bash">InstrumentationTestRunner</span></code> and <code class="codecolorer bash default"><span class="bash">GoogleInstrumentationTestRunner</span></code>, runs <em>JUnit3</em> and <em>JUnit4</em> tests against your Android application.</p>
<p>First add the dependencies to your <code class="codecolorer bash blackboard"><span class="bash">build.gradle</span></code>, assuming you have installed the <a href="https://developer.android.com/tools/support-library/index.html">Testing Support Library</a>.</p>
<div class="codecolorer-container groovy blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="groovy codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">dependencies <span style="color: #66cc66;">&#123;</span><br />
&nbsp; androidTestCompile <span style="color: #ff0000;">'com.android.support.test:testing-support-lib:0.1'</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>Then add the runner in your build.gradle<code class="codecolorer groovy default"><span class="groovy">android.<span style="color: #006600;">defaultConfig</span></span></code> configuration:</p>
<div class="codecolorer-container groovy blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="groovy codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">defaultConfig <span style="color: #66cc66;">&#123;</span><br />
&nbsp; ...<br />
&nbsp; <span style="color: #006600;">testInstrumentationRunner</span> <span style="color: #ff0000;">&quot;android.support.test.runner.AndroidJUnitRunner&quot;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>&nbsp;&nbsp;</p>
<h3>Writing the test</h3>
<p>As you may know test classes must be in <code class="codecolorer bash default"><span class="bash">src\androidTest\com.example.package.tests</span></code>, com.example.package being the package specified in the package attribute of the manifest element in the <code class="codecolorer bash default"><span class="bash">AndroidManifest</span></code> file.&nbsp;<br />
Also each test class must extend the abstract class <code class="codecolorer bash default"><span class="bash">ActivityInstrumentationTestCase2</span></code> and supply the Test Activity as generic type that will be used by default for testing.<br />
It must also be passed to the superclass via the <code class="codecolorer bash default"><span class="bash">super<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></span></code>. To make the Test Activity being called by the test framework, simply define a setup which calls the synchronous method <code class="codecolorer bash default"><span class="bash">getActivity<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.</span></code></p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FunctionalInstrumentationTest <span style="color: #000000; font-weight: bold;">extends</span> ActivityInstrumentationTestCase2<span style="color: #339933;">&lt;</span>ActivityToTest<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> FunctionalInstrumentationTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>ActivityToTest.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception"><span style="color: #003399;">Exception</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">setUp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; getActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>As mentioned we want to check if <em>‘6’ x ‘7’ equals ‘42’</em>.</p>
<div class="codecolorer-container java blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testAnswer_to_the_Ultimate_Question_of_Life_the_Universe_and_Everything<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;7&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;×&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;6&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>click<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; onView<span style="color: #009900;">&#40;</span>withId<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">resText</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">check</span><span style="color: #009900;">&#40;</span>matches<span style="color: #009900;">&#40;</span>withText<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;42&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>You may have noticed, this example is using static imports. This is solely done to make the code more readable.</p>
<p>Other actions you want to use are:</p>
<ul>
<li><code class="codecolorer java default"><span class="java">pressBack<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code> to simulate the use of the &#8220;back&#8221; button,</li>
<li><code class="codecolorer java default"><span class="java">isDisplayed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code> to check if an element is being shown and </li>
<li><code class="codecolorer java default"><span class="java">scrollTo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></span></code> to scroll to an element.</li>
</ul>
<p>&nbsp;&nbsp;</p>
<h3>Running the test</h3>
<p>Now lets do the fun part, lets run the test. This can be done from the command line with he command line with <code class="codecolorer bash blackboard"><span class="bash">gradle connectedAndroidTest</span></code> or using Android Studio by:</p>
<ol>
<li><em>Open Run menu | Edit Configurations</em></li>
<li><em>Add a new Android Tests configuration</em></li>
<li><em>Choose the module you are testing</em></li>
<li><em>Define our test runner: <code class="codecolorer bash default"><span class="bash">android.support.test.runner.AndroidJUnitRunner</span></code></em></li>
</ol>
<p>	<br />
<img src="http://i.giphy.com/10OjZwNlstPj9e.gif" width=100%"/><br />
<br />	<br />
So now you know a bit more about Espresso. If you want to know more, you may want to visit:</p>
<ul>
<li><a href="https://code.google.com/p/android-test-kit/">Espresso website</a></li>
<li><a href="https://github.com/welsinga/sample_espresso">Github repo corresponding to this article</a></li>
<li><a href="https://github.com/googlesamples/android-testing">General Espresso Github samples by Google</a></li>
</ul>
<p>
<strong>Updated</strong> (08-04) When running the tests with gradle you can just use <code class="codecolorer bash blackboard"><span class="bash">gradle connectedAndroidTest</span></code></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
