Android API 中的检测类

发布于 2024-08-28 08:04:15 字数 397 浏览 12 评论 0原文

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

可以使用 Junit 框架来测试 Android API 的方法,而无需使用 Instrumentation 类。

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

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

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

多谢。珍惜你的时间。

问候, 里亚斯

I have question on the Android API. Android API provides a class called "Instrumentation" class. What is the use of this class? Is the Instrumentation class be used only together with Junit for unit testing.

Can Junit framework can be used to test the methods of the Android API without using the Instrumentation class.

Since Junit package has been included in the Android package, I hope we dont need to use install separately for unit testing.

I would appreciate if you could provide me the information as i can't find these clear information anywhere on the Web.

If we use Junit test framework to test the Android API, can we have test results in the UI format rather than test format.?

Thanks a lot. Apprecite your time.

Regards,
Riyas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揽清风入怀 2024-09-04 08:04:15

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

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

这是链接< /a> 对各种测试类有一些相当好的描述。

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

The Instrumentation class allows the test case to subscribe to various application events (keypresses, etc), and also programmatically control the UI to enable functional testing of your application.

Therefore, you technically do not need the Instrumentation class for Junit testing if all you are doing is unit testing, and not functional testing of the UI. You could just extend TestCase which does not provide any Instrumentation support.

Here is a link with some fairly good descriptions of the various test classes.

Junit is included within Android and will not need to be installed separately.

固执像三岁 2024-09-04 08:04:15

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

Instrumentation API is basically used to inject various kinds of events to the UI so that the application can be stress-tested. You can create an arbitrary event like tapping on the screen and inject it using instrumentation API which is basically like a pseudo event generation.

掀纱窥君容 2024-09-04 08:04:15

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 框架/操作系统实现)。因此,您只需使用此类即可编写自己的全新测试框架的实现。

Instrumentation class is a base class for test frameworks, this provides a very specific functionality, It also provides API to invoke test on device/emulator using ActivityManager Interface via ADB. Also, it can gets back the results to the development machines.

By overriding this class you can enjoy all the context and helper classes you need to run your tests.

Below is a sample implementation, I wrote to explain you the functionality.

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);

    }
}

Manifest:

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

Now if you run this command from your development machine.

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

Your instrumentation class onCreate methods get called and you will get some binary encoded response on your console containing data from the bundle you passed to the 'finish' function.

"


whatisthis
thisiswhat

I hope this will make you understand the functionality specific to this base class. For the sake of some motivation, I would like to add that the new android testing framework androidx.test gets compiled with our app (not part of OS and can be modified) directly extends the Instrumentation class (part of Android Framework/OS implementation). Hence you can write your own implementation of a whole new testing framework just by using this class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文