What is the purpose of @SmallTest, @MediumTest, an

2019-01-29 19:05发布

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?

3条回答
混吃等死
2楼-- · 2019-01-29 19:35

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

查看更多
贼婆χ
3楼-- · 2019-01-29 19:43

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'
        }
    }

See this blog post for more details.

查看更多
smile是对你的礼貌
4楼-- · 2019-01-29 19:44

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.

See this page (search for "@SmallTest") on how to specify which tests get run.

查看更多
登录 后发表回答