What is the purpose of @SmallTest, @MediumTest, and @LargeTest annotations in Android?
JavaAndroidUnit TestingTddAndroid EspressoJava Problem Overview
I'm new to Android and I've seen example code using these annotations. For example:
@SmallTest
public void testStuff() {
TouchUtils.tapView(this, anEditTextView);
sendKeys("H E L P SPACE M E PERIOD");
assertEquals("help me.", anEditTextView.getText().toString());
}
What does that annotation accomplish?
Java Solutions
Solution 1 - Java
This blog post explains it best. Basically, it is the following:
- Small: this test doesn't interact with any file system or network.
- Medium: Accesses file systems on box which is running tests.
- Large: Accesses external file systems, networks, etc.
Per the Android Developers blog, a small test should take < 100ms, a medium test < 2s, and a large test < 120s.
The answer from azizbekian shows how to utilize the annotation when running your tests.
Also, this old out-of-date page has even more information. Specifically, how to use the am instrument
tool with adb shell
. Here's the pertinent parts:
am instrument options
The am instrument
tool passes testing options to InstrumentationTestRunner
or a subclass in the form of key-value pairs, using the -e flag, with this syntax:
-e <key> <value>
Some keys accept multiple values. You specify multiple values in a comma-separated list. For example, this invocation of InstrumentationTestRunner
provides multiple values for the package
key:
$ adb shell am instrument -w -e package com.android.test.package1,com.android.test.package2 \
> com.android.test/android.test.InstrumentationTestRunner
The following table describes the key-value pairs and their result. Please review the Usage Notes following the table.
Key | Value | Description |
---|---|---|
size | [small | medium | large] | Runs a test method annotated by size. The annotations are @SmallTest, @MediumTest, and @LargeTest. |
So reading the above, you could specify small tests like this:
$ adb shell am instrument -w \
> -e package com.android.test.package1,com.android.test.package2 \
> -e size small \
> com.android.test/android.test.InstrumentationTestRunner
Solution 2 - Java
As an addition to Davidann's answer and mainly OP's question in the comment:
> In the context of the code above, does it actually DO anything except leave a note for other developers? Does it enforce anything? Are there any tools that utilizes this annotation? What's it's purpose in Android development?
You can run a group of tests annotated with specific annotation.
From AndroidJUnitRunner documentation:
> Running a specific test size i.e. annotated with SmallTest or MediumTest or LargeTest:
> adb shell am instrument -w -e size [small|medium|large] com.android.foo/android.support.test.runner.AndroidJUnitRunner
You may also setup those params through gradle:
android {
...
defaultConfig {
...
testInstrumentationRunnerArgument 'size', 'Large'
}
}
Via gradle:
-Pandroid.testInstrumentationRunnerArguments.size=small
See Doug Stevenson blog post as well as this blog post for more details.
Solution 3 - Java
You can also annotate POJO unit tests with @Category(MediumTest.class)
or @Category(LargeTest.class)
, etc. by defining your own Categories - see the test-categories repo for an example