Today’s #AndroidTools #Protip from +Stephan Linzner is about android test size annotations and finding the right size for your next test.

Android test size annotations let you classify how long a test should take to run, and consequently, how frequently you can run the test. The annotations are defined in the android.test.suitebuilder.annotation package. To use one of the test size annotations @SmallTest, @MediumTest or @LargeTest simply annotate your test methods with the appropriate annotation, i.e. 

@SmallTest
public void testFoo_bar() { … }

A Small test should take milliseconds, a Medium test a couple of seconds, and Large several minutes. To make decisions easier here are some additional guidelines you can use to find the right test size next time you write a test.

@SmallTests 
Execution time: < 100ms, these tests should be run very frequently.
Focused on units of code to verify specific logical conditions. These tests should runs in an isolated environment and use mock objects for external dependencies. Resource access (such as file system, network, or databases) are not permitted. Tests that interact with hardware, make binder calls, or that facilitate android instrumentation should not use this annotation.

@MediumTests 
Execution time: < 2s, these tests run at least on every check in of your code.
Focused on a very limited subset of components or a single component. Resource access to the file system through well defined interfaces like databases, ContentProviders, or Context is permitted. Network access should be restricted to localhost only and (long-running) blocking operations should be avoided and use mock objects instead. Note that any child of InstrumentationTestCase is at least medium!

@LargeTests 
Execution time: < 120s, these tests should be run as often as practical. At least once a day.
Focused on any or all application components and subsystems. These tests fully participate in  the system and may make use of all resources such as databases, file systems, and network services. As a rule of thumb all external communications such as GPS, Bluetooth, Radio, and Telephony must be large tests. Most functional UI tests, that test across multiple components, are also large tests too.

Well organized tests suites using test size annotations can improve your efficiency and productivity and establish a common understanding within your team on when to use the right test annotation for the job. Think about it when you are writing your next test. 

If you want to learn more about Android Testing and how to run specific test suite for a given size, please visit: http://developer.android.com/tools/testing/testing_android.html and http://googletesting.blogspot.co.uk/2010/12/test-sizes.html.
Shared publiclyView activity