【问题标题】:Which is a better way to access Context in instrumented unit tests?在检测单元测试中访问上下文的更好方法是什么?
【发布时间】:2020-12-26 20:08:59
【问题描述】:

更推荐在插桩单元测试中访问Context:使用ApplicationProviderInstrumentationRegistry

val context: Context = ApplicationProvider.getApplicationContext()
import androidx.test.platform.app.InstrumentationRegistry

val context: Context = InstrumentationRegistry.getInstrumentation().targetContext

我知道androidx.test.InstrumentationRegistry 已被弃用,但androidx.test.platform.app.InstrumentationRegistry 不是。

【问题讨论】:

    标签: android androidx android-context ui-testing instrumented-test


    【解决方案1】:

    您可以根据您的 SDK 版本使用 androidx.test.platform.app.InstrumentationRegistry 或 android.support.test.InstrumentationRegistry 并通过以下方式获取上下文

    上下文上下文 = InstrumentationRegistry.getInstrumentation().getTargetContext();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      • 2013-07-03
      • 2016-06-25
      • 1970-01-01
      • 2010-09-08
      • 1970-01-01
      • 2010-10-20
      相关资源
      最近更新 更多