Since I first had trouble understanding how to test Apps on Android or when to use which class with the Android Testing API, I want to share my new knowledge and hope that you will correct me if I got someting wrong.
TestCase
/ \
AndroidTestCase InstrumentationTestCase
/ | \ / \
ApplicationTC ProviderTC2 ServiceTC SingleLaunchTC ActivityTestCase
/ \
ActivityUnitTC ActivityInstrumentationTC
TestCase: Plain old JUnit test case. It can test utility classes that are not tied to the Android framework.
AndroidTestCase: It extends JUnit’s TestCase. Its getContext() method allows you to get an injected context if you need one. Since you can get a context from this class, you can inflate your UI objects to test their behaviors. However, the UI behaviors will not show in the emulator. I like to use this class to test my data access objects.
Application TestCase: It provides testing for Application classes. It can be used to test the life cycle of an application. This framework allows you to inject a modified, mock, or isolated replacement for this dependencies (Context), and thus perform a true unit test.
Provider TestCase2: It provides testing for each ContentProvider. It allows you to test your content provider in an isolated environment. Android mock objects such as IsolatedContext and MockContentResolver also help provide an isolated test environment. More info: http://ift.tt/1Q7jmQd
Service TestCase: Android provides a testing framework for Service objects that can run them in isolation and provides mock objects. Since the Service class assumes that it is separate from its clients, you can test a Service object without using instrumentation. More info: http://ift.tt/1EgyPXN
Instrumentation: Collection of control methods in the Android System. (onCreate, onStart .. )
Instrumentation TestCase: It enables you to call the getInstrumentation method to get an instance of instrumentation so that you can operate application, activity, and so on. The Instrumentation class allows the test case to subscribe to various application events (keypresses, etc), and also programmatically control the UI to enable functional testing of your application.Functional testing of the UI. Instrumentation API is basically used to inject various kinds of events to the UI so that the application can be stress-tested.
SingleLaunchActivity TestCase SingleLaunchActivityTestCase(SLATC) is primarily for situations where you want to simulate a non-standard mode. (singleTop, singleTask, singleInstance). The three non-standard modes share a characteristic that, depending on the circumstances, there will only be a single instance of the Activity. SLATC forces a single instance for multiple test methods. You can use it to either do multiple different tests on the same instance, or run the same test multiple times.
ActivityUnitTestCase: It gives the tested activity an isolated environment. When using it to test an activity, the activity is not attached to the system. This gives you more control over what kind of environment that you want your activity to be tested in.
ActivityInstrumentationTestCase2: It’s a heavier testing class compared to AndroidTestCase. It provides UI and functional testing for a single activity. You can get an injected activity that you are testing on by calling its getActivity() method. The activity being tested is launched and finished before and after each test.
InstrumentationTestRunner: Driver to run tests on your target application
InstrumentationTestSuite: TestSuite, which injects Instrumentation before running the tests.
Aucun commentaire:
Enregistrer un commentaire