Android中@SmallTest,@ MediumTest和@LargeTest注释的目的是什么?

问题描述 投票:90回答:3

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

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

那个注释有什么作用?

java android unit-testing tdd android-espresso
3个回答
123
投票

This blog post解释得最好。基本上,它是以下内容:

testing chart

  1. 小:此测试不与任何文件系统或网络交互。
  2. 中:访问正在运行测试的框上的文件系统。
  3. 大:访问外部文件系统,网络等。

根据Android Developers blog,小测试应该<100ms,中等测试<2s,大测试<120s。

有关如何指定运行哪些测试的信息,请参阅this page(搜索“@SmallTest”)。


7
投票

作为评论中Davidann's answerOP's question的补充:

在上面的代码的上下文中,除了为其他开发人员留下注释之外,它实际上做了什么吗?它执行什么吗?有没有使用此注释的工具? Android开发的目的是什么?

您可以运行一组使用特定注释注释的测试。

来自AndroidJUnitRunner documentation

运行特定的测试大小,即使用SmallTest或MediumTest或LargeTest进行注释:

adb shell am instrument -w -e size [small | medium | large] com.android.foo/android.support.test.runner.AndroidJUnitRunner

您也可以通过gradle设置这些参数:


    android {
        ...
        defaultConfig {
            ...
            testInstrumentationRunnerArgument 'size', 'Large'
        }
    }

有关详细信息,请参阅this blog post


1
投票

您还可以通过定义自己的@Category(MediumTest.class)来使用@Category(LargeTest.class)Categories等注释POJO单元测试 - 请参阅test-categories repo以获取示例

© www.soinside.com 2019 - 2024. All rights reserved.