1 / 24

Testing with Android Part I of II

Testing with Android Part I of II. Android Testing Framework. Based on JUnit The Android JUnit extensions provide component-specific test case classes. . Android Testing Framework. Test suites are contained in test packages that are similar to main application packages.

velika
Download Presentation

Testing with Android Part I of II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Testing with Android Part I of II

  2. Android Testing Framework • Based on JUnit • The Android JUnit extensions provide component-specific test case classes.

  3. Android Testing Framework • Test suites are contained in test packages that are similar to main application packages. • The SDK tools for building and tests are available in Eclipse with ADT, and also in command-line form for use with other IDES.

  4. Android Testing Framework • These tools get information from the project of the application under test and use this information to automatically create the build files, manifest file, and directory structure for the test package. • The SDK also provides monkeyrunner, an API testing devices with Python programs, and UI/Application Exerciser Monkey, a command-line tool for stress-testing UIs by sending pseudo-random events to a device.

  5. Android Testing Framework

  6. JUnit • You can use the JUnitTestCase class to do unit testing on a plain Java object. • TestCaseis also the base class for AndroidTestCase, which you can use to test Android-dependent objects. • Besides providing the JUnit framework, AndroidTestCase offers Android-specific setup, teardown, and helper methods.

  7. Testing from Eclipse with ADT ADT provides several features that help you set up and manage your testing environment effectively: • It lets you quickly create a test project and link it to the application under test. • It lets you quickly import the classes of the application under test. • It lets you create run configurations for your test package and include in them flags that are passed to the Android testing framework. • It lets you run your test package without leaving Eclipse.

  8. Creating a Test Project

  9. The class contains definitions for four methods: • HelloAndroidTest: This defines the constructor for the class. It is required by the Android testing framework. • setUp(): This overrides the JUnitsetUp() method. You use it to initialize the environment before each test runs. • testPreconditions(): This defines a small test that ensures the Hello, Android application starts up correctly. • testText(): This tests that what is displayed on the screen is the same as what is contained in the application's string resources. It is an example of a real unit test you would perform against an application's UI.

  10. Creating the Test Case Class

  11. HelloAndroidTest.java package com.example.helloandroid.test; import android.test.ActivityInstrumentationTestCase2; import com.example.helloandroid.HelloAndroid; public class HelloAndroidTest extends ActivityInstrumentationTestCase2<HelloAndroid> { }

  12. Constructor private HelloAndroidmActivity; private TextViewmView; private String resourceString; public HelloAndroidTest() {       super("com.example.helloandroid",HelloAndroid.class);     }

  13. setUp() @Override protected void setUp() throws Exception { super.setUp(); mActivity = this.getActivity(); mView = (TextView) mActivity.findViewById (com.example.helloandroid.R.id.textview); resourceString = mActivity.getString (com.example.helloandroid.R.string.hello); }

  14. Adding a preconditions test A preconditions test checks the initial application conditions prior to executing other tests. public void testPreconditions() { assertNotNull(mView); }

  15. Adding a unit test public void testText() { assertEquals(resourceString,(String)mView.getText()); }

  16. Finished Class - 1 package com.example.helloandroid.test; import com.example.helloandroid.HelloAndroid; import android.test.ActivityInstrumentationTestCase2; import android.widget.TextView; public class HelloAndroidTest extends ActivityInstrumentationTestCase2<HelloAndroid> { private HelloAndroidmActivity; // the activity under test private TextViewmView; // the activity's TextView (the only view) private String resourceString; public HelloAndroidTest() { super("com.example.helloandroid", HelloAndroid.class); } }

  17. Finished Class @Override protected void setUp() throws Exception { super.setUp(); mActivity = this.getActivity(); mView = (TextView) mActivity.findViewById(com.example.helloandroid.R.id.textview); resourceString = mActivity.getString(com.example.helloandroid.R.string.hello); } public void testPreconditions() { assertNotNull(mView); } public void testText() { assertEquals(resourceString,(String)mView.getText()); } }

  18. Running Tests • When you run a test package in Eclipse with ADT, the output appears in the Eclipse JUnit view. • You can run the entire test package or one test case class. To do run tests, Eclipse runs the adb command for running a test package, and displays the output, so there is no difference between running tests inside Eclipse and running them from the command line.

More Related