Android 中@SmallTest、@MediumTest 和@LargeTest 注释的用途是什么?

我是 Android 的新手,我看到过使用这些注释的示例代码,例如:

@SmallTest
public void testStuff() {
TouchUtils.tapView(this, anEditTextView);
sendKeys("H E L P SPACE M E PERIOD");
assertEquals("help me.", anEditTextView.getText().toString());
}

这个注释实现了什么目的?

24626 次浏览

This blog post explains it best. Basically, it is the following:

testing chart

  1. Small: this test doesn't interact with any file system or network.
  2. Medium: Accesses file systems on box which is running tests.
  3. 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


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.

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