Top Banner
ANDROID – TESTING L. Grewe
22

ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

Dec 31, 2015

Download

Documents

Meryl Golden
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

ANDROID – TESTING

L. Grewe

Page 2: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

With the AndroidStudio IDE

Page 3: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

Setup Configurations

STEP 1:Run->Edit Configurations

STEP 2: Hit + in Config window and add Android Tests

Page 4: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

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.

Page 5: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

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

Page 6: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

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 )

Page 7: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

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.

Page 8: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

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()); }}

Page 9: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

Testing using android.test

Here creating classes (extending) from existing classes in android.test

Page 10: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

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

Page 11: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

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.

Page 12: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

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

Page 13: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

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.

Page 14: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

Simply run the config you setup earlier (see start of talk)

Running tests

Page 15: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

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

Page 16: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

With the Eclipse IDE

Page 17: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

Create a “Test Project”

File->New->Android Test Project (find it)

Give a Name Select Target –the Android Project you are going to test

Page 18: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

Create new Junit Test class

In src/package “New->Junit Test Case

Page 19: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

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()); }}

Page 20: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

Running the Junit tests

Right click and select Run As -> Android JUnit Test

Page 21: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

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>

Page 22: ANDROID – TESTING L. Grewe. With the AndroidStudio IDE.

Naming your Test JUnit classes If testing a class called MainActivity call

you Junit class MainActivity_Test or MainActivity_TestCase