android – testing l. grewe. with the androidstudio ide
TRANSCRIPT
Setup Configurations
STEP 1:Run->Edit Configurations
STEP 2: Hit + in Config window and add Android Tests
Setup continued
Step 3: Now configure Testing configuration to point to the module of your app
select“All in Module” and Android Studiowill automaticallyfind any test insideyour whole Module! You can also get morespecific and select “Class” or even “Method” option to narrow the scope of your testing down further.
All test methods MUST start with the “test-” prefix or Android Studio will not detect them as tests and you will get all kinds of weird errors and nothing will work.
Setup finished
Step 4: after saving config in Step 3 you can see it is an option for running (now we have the app itself and the testing classes) NEXT how do we make the testing code
When you create Project –you have testing directory preset up. Look at this example
for a simple HelloWorld app
Can also create test directory under the package (here com.example.lynne.hello then would be called come.example.lynne.hello.test )
Writing Test classes - Based on JUnit use plain JUnit to test a class that doesn't call
the Android API, JUnit TestCase class to do unit testing on a class that
doesn't call Android APIs.
Use Android's JUnit extensions to test Android components. Android JUnit extensions provide component-specific
test case classes. provide helper methods for creating mock objects
and methods that help you control the lifecycle of a component.
Testing PLO (plain old java object) Plain Old Java Objects
(i.e. independent of frameworks like Android or J2EE)
Test using assertX = assertTrue, assertEquals to see if value is true or euqals some value.
import junit.framework.TestCase;import com.android.lab4.Joke;
public class JokeTest extends TestCase {
public void testJoke() { Joke joke = new Joke(); assertTrue("m_strJoke should be initialized to \"\".", joke.getJoke().equals("")); assertTrue("m_strAuthorName should be initialized to \"\".", joke.getAuthor().equals("")); assertEquals("m_nRating should be initialized to Joke.UNRATED.", Joke.UNRATED, joke.getRating()); }}
AndroidTestCase class http://developer.android.com/reference/android/test/AndroidTestCase.html
void assertActivityRequiresPermission(String packageName, String className, String permission)Asserts that launching a given activity is protected by a particular permission by attempting to start the activity and validating that a SecurityException is thrown that mentions the permission in its error message.
void assertReadingContentUriRequiresPermission(Uri uri, String permission)Asserts that reading from the content uri requires a particular permission by querying the uri and ensuring a SecurityException is thrown mentioning the particular permission.
void assertWritingContentUriRequiresPermission(Uri uri, String permission)Asserts that writing to the content uri requires a particular permission by inserting into the uri and ensuring a SecurityException is thrown mentioning the particular permission.
Context getContext()void setContext(Context context)void testAndroidTestCaseSetupProperly()void setup() and teardown() extends TestCase and Assert, which you can use to test Android-
dependent objects. • AndroidTestCase offers Android-specific setup, teardown, and helper
methods.• testing permissions
ApplicationTest class
ApplicationTestCase test case class to test the setup and
teardown of Application objects. can be useful in verifying that the
<application> element in the manifest file is correctly set up. Note, however, that this test case does not allow you to control testing of the components within your application package.
Many other classes in android.test
InstrumentationClass ActivityUnitTestCase ActivityInstrumentaitonTestCase*
LOOK AT METHODS……read Android guide to testing online
http://developer.android.com/tools/testing/testing_android.html
Testing Activities ---read this http://developer.android.com/tools/testing/activity_testing.html
What would some real test code look like…suppose you have Activity with Spinner
public class myActivityTest extends ActivityInstrumentationTestCase2{
public void test() throws Exception {
// Start the main activity of the application under test—gets this from parent class mActivity = getActivity();
// Get a handle to the Activity object's main UI widget, a Spinner mSpinner = (Spinner)mActivity.findViewById(com.android.example.spinner.R.id.Spinner01);
// Set the Spinner to a known position mActivity.setSpinnerPosition(TEST_STATE_DESTROY_POSITION);
// Stop the activity - The onDestroy() method should save the state of the Spinner mActivity.finish();
// Re-start the Activity - the onResume() method should restore the state of the Spinner mActivity = getActivity();
// Get the Spinner's current position int currentPosition = mActivity.getSpinnerPosition();
// Assert that the current position is the same as the starting position assertEquals(TEST_STATE_DESTROY_POSITION, currentPosition);
How does this connect to theactual Activity class you want to test
THROUGH the constructorpublic ActivityInstrumentationTestCase2 (Class<T> activityClass)
where the parameter is the class you want to test
All test methods MUST start with the “test-” prefix or Android Studio will not detect them as tests and you will get all kinds of weird errors and nothing will work.
More on your own
Only touched the tip Other apis part of android.test
Testing UIs Testing Services Testing ContentProviders
See http://developer.android.com/tools/testing/index.html
Create a “Test Project”
File->New->Android Test Project (find it)
Give a Name Select Target –the Android Project you are going to test
Testing PLO (plain old java object) Plain Old Java Objects
(i.e. independent of frameworks like Android or J2EE)
Test using assertX = assertTrue, assertEquals to see if value is true or euqals some value.
import junit.framework.TestCase;import com.android.lab4.Joke;
public class JokeTest extends TestCase {
public void testJoke() { Joke joke = new Joke(); assertTrue("m_strJoke should be initialized to \"\".", joke.getJoke().equals("")); assertTrue("m_strAuthorName should be initialized to \"\".", joke.getAuthor().equals("")); assertEquals("m_nRating should be initialized to Joke.UNRATED.", Joke.UNRATED, joke.getRating()); }}
Test Project Manifest File
Note <instrumentation **> targets the project’s package you want to test
Note places it in same packagename.test<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloworld.test" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="19" />
<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.helloworld" />
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <uses-library android:name="android.test.runner" /> </application>
</manifest>