Download presentation
Presentation is loading. Please wait.
Published byMeryl Golden Modified over 9 years ago
1
ANDROID – TESTING L. Grewe
2
With the AndroidStudio IDE
3
Setup Configurations STEP 1:Run->Edit Configurations STEP 2: Hit + in Config window and add Android Tests
4
Setup continued Step 3: Now configure Testing configuration to point to the module of your app select“All in Module” and Android Studio will automatically find any test inside your whole Module! You can also get more specific 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.
5
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
6
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 )
7
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.TestCase 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.
8
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()); }
9
Testing using android.test Here creating classes (extending) from existing classes in android.test
10
AndroidTestCase class http://developer.android.com/reference/android/test/AndroidTestCase.html http://developer.android.com/reference/android/test/AndroidTestCase.html voidassertActivityRequiresPermissionassertActivityRequiresPermission(String packageName, String className, String permissi on)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.String SecurityException voidassertReadingContentUriRequiresPermissionassertReadingContentUriRequiresPermission(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.UriStringSecurityException voidassertWritingContentUriRequiresPermissionassertWritingContentUriRequiresPermission(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.UriStringSecurityException ContextgetContextgetContext() voidsetContextsetContext(Context context)Context voidtestAndroidTestCaseSetupProperlytestAndroidTestCaseSetupProperly() voidsetup() 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
11
ApplicationTest class ApplicationTestCase ApplicationTestCase test case class to test the setup and teardown of Application objects.Application can be useful in verifying that the 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.
12
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_andr oid.html http://developer.android.com/tools/testing/testing_andr oid.html Testing Activities ---read this http://developer.android.com/tools/testing/activity_testing.html http://developer.android.com/tools/testing/activity_testing.html
13
What would some real test code look like… suppose you have Activity with Spinner public class myActivityTest extends ActivityInstrumentationTestCase2 {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 the actual Activity class you want to test THROUGH the constructor public ActivityInstrumentationTestCase2 (Class activityClass)Class 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.
14
Simply run the config you setup earlier (see start of talk) Running tests
15
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 http://developer.android.com/tools/testing/index.html
16
With the Eclipse IDE
17
Create a “Test Project” File->New->Android Test Project (find it) Give a Name Select Target –the Android Project you are going to test
18
Create new Junit Test class In src/package “New->Junit Test Case
19
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()); }
20
Running the Junit tests Right click and select Run As -> Android JUnit Test
21
Test Project Manifest File Note targets the project’s package you want to test Note places it in same packagename.test <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloworld.test" android:versionCode="1" android:versionName="1.0" > <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.helloworld" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >
22
Naming your Test JUnit classes If testing a class called MainActivity call you Junit class MainActivity_Test or MainActivity_TestCase
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.