gpt4 book ai didi

android - 使用 Gradle 测试 Android 库

转载 作者:行者123 更新时间:2023-11-29 17:52:41 26 4
gpt4 key购买 nike

测试 Android 库的正确流程是什么?我的图书馆严重依赖 android.location 和 Google Play Services 融合位置提供程序,因此我希望能够测试位置更改。

src下添加instrumentTest文件夹后,怎么办? Gradle 用户指南 simply says像常规应用程序一样测试库,但由于我没有 Activity 如何实例化我的库类?

最佳答案

你应该使用 InstrumentationTestCase (和子类,取决于您的需要)。

您可以通过在测试用例中调用 getInstrumentation().getContext() 来获取 Context

使用 Gradle 时,您只需将测试类放在 /src/instrumentTest 中,然后通过创建 Android 测试 运行配置来运行它们。您还可以从命令行运行测试:./gradlew check(或者,其中一种变体,例如 connectedCheckconnectedInstrumentTest) .

最后,您可能应该模拟所有外部依赖项。您可能想看看一些模拟框架,例如 Mockito,或者自己动手做。

关于android - 使用 Gradle 测试 Android 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21880482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com