有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 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());
}

这个注释有什么作用


共 (3) 个答案

  1. # 1 楼答案

    This blog post解释得最好。基本上是这样的:

    testing chart

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

    根据Android Developers blog,应该进行一次小型测试<;100毫秒,中等测试<;2s和一个大型测试<;120秒

    关于如何指定运行哪些测试,请参见this page(搜索“@SmallTest”)

  2. # 2 楼答案

    作为评论中Davidann's answer和主要OP's question的补充:

    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?

    可以运行一组带有特定注释的测试

    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

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

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

    Via gradle:

    -Pandroid.testInstrumentationRunnerArguments.size=small
    

    有关更多详细信息,请参见Doug Stevenson blog postthis blog post

  3. # 3 楼答案

    您还可以通过定义自己的Categories来用@Category(MediumTest.class)@Category(LargeTest.class)等来注释POJO单元测试——有关示例,请参见test-categoriesrepo