【问题标题】:Instrumentation class in the Android APIAndroid API 中的检测类
【发布时间】:2011-02-04 18:13:51
【问题描述】:

我对 Android API 有疑问。 Android API 提供了一个名为“Instrumentation”的类。这个类有什么用? Instrumentation 类是否只能与 Junit 一起用于单元测试。

Junit 框架能否在不使用 Instrumentation 类的情况下测试 Android API 的方法。

由于 Android 包中已经包含了 Junit 包,希望我们不需要单独使用 install 进行单元测试。

如果您能向我提供这些信息,我将不胜感激,因为我在网络上的任何地方都找不到这些明确的信息。

如果我们使用Junit测试框架来测试Android API,我们可以得到UI格式而不是测试格式的测试结果吗?

非常感谢。珍惜你的时间。

问候, 里亚斯

【问题讨论】:

    标签: android junit instrumentation


    【解决方案1】:

    Instrumentation 类允许测试用例订阅各种应用程序事件(按键等),并以编程方式控制 UI 以启用应用程序的功能测试。

    因此,如果您所做的只是单元测试,而不是 UI 的功能测试,那么从技术上讲,您不需要 Instrumentation 类来进行 Junit 测试。您可以只扩展不提供任何 Instrumentation 支持的 TestCase

    这是一个link,其中包含对各种测试类的一些相当好的描述。

    Junit 包含在 Android 中,不需要单独安装。

    【讨论】:

      【解决方案2】:

      Instrumentation API 主要用于向 UI 注入各种事件,以便对应用程序进行压力测试。您可以创建一个任意事件,例如点击屏幕,然后使用检测 API 注入它,这基本上就像一个伪事件生成。

      【讨论】:

        【解决方案3】:

        Instrumentation 类是测试框架的基类,它提供了非常具体的功能,它还提供 API 以通过 ADB 使用 ActivityManager 接口调用设备/模拟器上的测试。此外,它还可以将结果返回给开发机器。

        通过覆盖这个类,您可以享受运行测试所需的所有上下文和辅助类。

        下面是一个示例实现,我写信是为了向您解释功能。

        public class SampleInstrumentation extends Instrumentation {
        
            @Override
            public void onCreate(Bundle arguments) {
                super.onCreate(arguments);
                Bundle bundle = new Bundle();
                bundle.putString("whatisthis","thisiswhat");
                finish(0,bundle);
        
            }
        }
        
        

        清单:

        ...
        ...
            </application>
            <instrumentation
                android:name=".SampleInstrumentation"
                android:targetPackage="com.commons"></instrumentation>
        </manifest>
        

        现在如果你从你的开发机器上运行这个命令。

        adb shell am instrument -w -m com.commons/com.commons.SampleInstrumentation
        

        您的检测类 onCreate 方法被调用,您将在控制台上获得一些二进制编码响应,其中包含您传递给“完成”函数的捆绑包中的数据。

        "
        
        
        whatisthis
        thisiswhat
        
        

        我希望这将使您了解特定于该基类的功能。为了一些动机,我想补充一点,新的android测试框架androidx.test是用我们的应用程序编译的(不是操作系统的一部分,可以修改)直接扩展了Instrumentation类(Android框架/操作系统实现的一部分) .因此,您只需使用此类即可编写自己的全新测试框架实现。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-16
          • 2013-02-05
          • 2011-12-01
          • 2018-09-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多