java.lang.IllegalStateException: 没有注册仪器!必须 运行 在注册工具下
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation
我一直在尝试使用 Espresso 执行简单的 UI 测试,但我的所有测试都失败了,并出现相同的异常:
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation
这是一份使用 esspresso 的初学者指南 here. I have already found similar questions but the most relevant ones to me are unanswered here - 我想这是因为他们没有画出全貌,所以这是我的代码。我将只展示一个测试,因为它们都失败并出现完全相同的错误:
build.gradle(模块:app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "io.github.vinge1718.myrestaurants"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
dependencies {
testImplementation "org.robolectric:robolectric:3.8"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
}
build.gradle(项目:MyRestaurant)
buildscript {
repositories {
google()
jcenter()
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
这里的更正是两个测试。我不认为该错误与测试本身有任何关系,而是与配置有关 - 我同意纠正
(MainActivityInstrumentationTest.java)
package io.github.vinge1718.myrestaurants;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import androidx.test.rule.ActivityTestRule;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
@RunWith(AndroidJUnit4.class)
public class MainActivityInstrumentationTest {
private String mStringToBetyped;
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);
@Before
public void initValidString() {
// Specify a valid string.
mStringToBetyped = "Portland";
}
@Test
public void validateEditText(){
onView(withId(R.id.locationEditText))
.perform(typeText(mStringToBetyped), closeSoftKeyboard())
.check(matches(withText(mStringToBetyped)));
}
@Test
public void locationIsSentToRestaurantActivity(){
String location = "Portland";
onView(withId(R.id.locationEditText)).perform(typeText(location));
onView(withId(R.id.findRestaurantsButton)).perform(click());
onView(withId(R.id.locationTextView)).check(matches(withText("Here are all the Restaurants near " + location)));
}
}
我已经尝试按照这个 espresso 设置文档 here 但我总是遇到同样的错误:
Started running tests
java.lang.IllegalStateException: No instrumentation registered! Must
run under a registering instrumentation. at
androidx.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:50)
at
androidx.test.InstrumentationRegistry.getTargetContext(InstrumentationRegistry.java:101)
at
androidx.test.rule.ActivityTestRule.(ActivityTestRule.java:144)
at
androidx.test.rule.ActivityTestRule.(ActivityTestRule.java:120)
at
androidx.test.rule.ActivityTestRule.(ActivityTestRule.java:103)
at
io.github.vinge1718.myrestaurants.MainActivityInstrumentationTest.(MainActivityInstrumentationTest.java:25)
at java.lang.reflect.Constructor.newInstance0(Native Method) at
java.lang.reflect.Constructor.newInstance(Constructor.java:334) at
org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at
org.junit.runners.BlockJUnit4ClassRunner.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at
org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290) at
org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at
org.junit.runners.ParentRunner.access[=16=]0(ParentRunner.java:58) at
org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at
org.junit.runners.ParentRunner.run(ParentRunner.java:363) at
android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
at org.junit.runners.Suite.runChild(Suite.java:128) at
org.junit.runners.Suite.runChild(Suite.java:27) at
org.junit.runners.ParentRunner.run(ParentRunner.java:290) at
org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at
org.junit.runners.ParentRunner.access[=16=]0(ParentRunner.java:58) at
org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at
org.junit.runners.ParentRunner.run(ParentRunner.java:363) at
org.junit.runner.JUnitCore.run(JUnitCore.java:137) at
org.junit.runner.JUnitCore.run(JUnitCore.java:115) at
android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at
android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at
android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2075)
Tests ran to completion.
这是我在浓缩咖啡设置中描述的测试配置 documentation:
我遇到了同样的问题,在 类 中练习方法的测试之间存在差异,那些与按钮交互、编辑文本等的标准单元 tests go in app/src/test/java/<package>
. UI tests 需要 expresso 并进入 app/src/androidTest/java/<package>
。我看了好几遍文档,浪费了一天的时间才弄明白其中的区别。
|____app
|
| ____src
| |____androidTest
| | |____java
| | |____<package>
| | |____ MainActivityInstrumentationTest.java # expresso here
| |____test
| |____java
| |____<package>
| |____ MainActivityInstrumentationTest.java # not here
我认为这是因为库 androidx
与 com.android.support.test
冲突。如果你想使用 jetpack,你必须将所有测试库转换为 androidx,如果你不想那样,只需删除你的 androidx
库并使用所有 com.android.support.test
。查看我在 中的最新答案。希望对你有帮助。
对我有用的是完全从
变回来
androidX
到
com.android.support
build.gradle
中的图书馆
请注意:您可能需要重新导入 class 中的库,例如
import androidx.test.runner.AndroidJUnitRunner;
至
import android.support.test.runner.AndroidJUnitRunner;
还要确保在每次从 build.gradle 添加或删除库后始终清理并重建项目。
我一直在尝试使用 Espresso 执行简单的 UI 测试,但我的所有测试都失败了,并出现相同的异常:
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation
这是一份使用 esspresso 的初学者指南 here. I have already found similar questions but the most relevant ones to me are unanswered here - 我想这是因为他们没有画出全貌,所以这是我的代码。我将只展示一个测试,因为它们都失败并出现完全相同的错误:
build.gradle(模块:app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "io.github.vinge1718.myrestaurants"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
dependencies {
testImplementation "org.robolectric:robolectric:3.8"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
}
build.gradle(项目:MyRestaurant)
buildscript {
repositories {
google()
jcenter()
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
这里的更正是两个测试。我不认为该错误与测试本身有任何关系,而是与配置有关 - 我同意纠正
(MainActivityInstrumentationTest.java)
package io.github.vinge1718.myrestaurants;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import androidx.test.rule.ActivityTestRule;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
@RunWith(AndroidJUnit4.class)
public class MainActivityInstrumentationTest {
private String mStringToBetyped;
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);
@Before
public void initValidString() {
// Specify a valid string.
mStringToBetyped = "Portland";
}
@Test
public void validateEditText(){
onView(withId(R.id.locationEditText))
.perform(typeText(mStringToBetyped), closeSoftKeyboard())
.check(matches(withText(mStringToBetyped)));
}
@Test
public void locationIsSentToRestaurantActivity(){
String location = "Portland";
onView(withId(R.id.locationEditText)).perform(typeText(location));
onView(withId(R.id.findRestaurantsButton)).perform(click());
onView(withId(R.id.locationTextView)).check(matches(withText("Here are all the Restaurants near " + location)));
}
}
我已经尝试按照这个 espresso 设置文档 here 但我总是遇到同样的错误:
Started running tests
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation. at androidx.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:50) at androidx.test.InstrumentationRegistry.getTargetContext(InstrumentationRegistry.java:101) at androidx.test.rule.ActivityTestRule.(ActivityTestRule.java:144) at androidx.test.rule.ActivityTestRule.(ActivityTestRule.java:120) at androidx.test.rule.ActivityTestRule.(ActivityTestRule.java:103) at io.github.vinge1718.myrestaurants.MainActivityInstrumentationTest.(MainActivityInstrumentationTest.java:25) at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:334) at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217) at org.junit.runners.BlockJUnit4ClassRunner.runReflectiveCall(BlockJUnit4ClassRunner.java:266) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner.run(ParentRunner.java:290) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access[=16=]0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner.run(ParentRunner.java:290) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access[=16=]0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2075)
Tests ran to completion.
这是我在浓缩咖啡设置中描述的测试配置 documentation:
我遇到了同样的问题,在 类 中练习方法的测试之间存在差异,那些与按钮交互、编辑文本等的标准单元 tests go in app/src/test/java/<package>
. UI tests 需要 expresso 并进入 app/src/androidTest/java/<package>
。我看了好几遍文档,浪费了一天的时间才弄明白其中的区别。
|____app
|
| ____src
| |____androidTest
| | |____java
| | |____<package>
| | |____ MainActivityInstrumentationTest.java # expresso here
| |____test
| |____java
| |____<package>
| |____ MainActivityInstrumentationTest.java # not here
我认为这是因为库 androidx
与 com.android.support.test
冲突。如果你想使用 jetpack,你必须将所有测试库转换为 androidx,如果你不想那样,只需删除你的 androidx
库并使用所有 com.android.support.test
。查看我在
对我有用的是完全从
变回来androidX
到
com.android.support
build.gradle
中的图书馆请注意:您可能需要重新导入 class 中的库,例如
import androidx.test.runner.AndroidJUnitRunner;
至
import android.support.test.runner.AndroidJUnitRunner;
还要确保在每次从 build.gradle 添加或删除库后始终清理并重建项目。