简体   繁体   English

Android 仪器测试:没有仪器注册错误

[英]Android Instrumentation Testing: No instrumentation registered Error

I am trying to write Android Instrumented test using Espresso.我正在尝试使用 Espresso 编写 Android Instrumented 测试。 When I run the test I get this error当我运行测试时,我收到此错误

java.lang.IllegalStateException: No instrumentation registered! java.lang.IllegalStateException:未注册检测! Must run under a registering instrumentation.必须在注册仪器下运行。

I can't understand what this error means.我不明白这个错误是什么意思。

I will briefly explain what I am doing in my test.我将简要解释我在测试中所做的事情。 I followed the samples given by google and EspressoTesting Tutorial我按照google和EspressoTesting Tutorial给出的样本

@SmallTest
@RunWith(AndroidJUnit4.class)
public class ActivityInputTest {

   @Rule
   public ActivityTestRule<ActivityInput> aiRule = new ActivityTestRule<>(ActivityInput.class);


   @Before
   public void setUp(){}
}

When I run the test the error occurs at annotation @Rule .当我运行测试时,错误发生在注释@Rule Can I get some explanation why this happens or what is the issue behind this?我能得到一些解释为什么会发生这种情况或者这背后的问题是什么? I am not interested in working code but more on the actual problem behind the issue.我对工作代码不感兴趣,但更多地关注问题背后的实际问题。

I also experienced error exactly same with you, because I used androidx library.我也遇到了与您完全相同的错误,因为我使用了androidx库。 I suggest you to check your libraries in build.gradle and change your dependencies for UI test like these block codes:我建议您检查 build.gradle 中的库并更改 UI 测试的依赖项,例如这些块代码:

androidTestImplementation "com.android.support.test:runner:1.0.2"
androidTestImplementation "com.android.support.test:rules:1.0.2"
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

Choose one of them, using com.android.support.test , or androidx library, because if we use them together, they will conflict each other.选择其中之一,使用com.android.support.testandroidx库,因为如果我们一起使用它们,它们会相互冲突。 Hope this can help you to resolve error No instrumentation registered! Must run under a registering instrumentation.希望这可以帮助您解决错误未No instrumentation registered! Must run under a registering instrumentation. No instrumentation registered! Must run under a registering instrumentation.

Changing how I retrieved the context from:更改我从以下位置检索上下文的方式:

@Before
public void setUp() {
    context = ApplicationProvider.getApplicationContext();
}

to the deprecated way :到弃用的方式:

@Before
public void setUp() {
    context = RuntimeEnvironment.application;
}

solved the issue for me.为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM