我一直在尝试使用Espresso执行简单的UI测试,我的所有测试都失败并出现相同的异常:
java.lang.IllegalStateException:没有注册的工具!必须在注册仪器下运行
这是使用esspresso here的初学者指南。我已经找到了类似的问题,但对我来说最相关的问题是没有答案的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
}
这里的修正是两个测试。我不认为错误与测试本身有任何关系,但配置 - 我现在已经纠正了
(main activity instrumentation test.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)));
}
}
我试过跟随这个浓缩咖啡设置文档here但我一直得到同样的错误:
开始运行测试
java.lang.IllegalStateException:没有注册的工具!必须在注册仪器下运行。 androidx.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:50)在androidx.test的androidx.test.InstrumentationRegistry.getTargetContext(InstrumentationRegistry.java:101)androidx.test.rule.ActivityTestRule。(ActivityTestRule.java:144)位于java.lang的io.github.vinge1718.myrestaurants.MainActivityInstrumentationTest。(MainActivityInstrumentationTest.java:25)上的androidx.test.rule.ActivityTestRule。(ActivityTestRule.java:103).rule.ActivityTestRule。(ActivityTestRule.java:120)位于org.junit.runners的org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)的java.lang.reflect.Constructor.newInstance(Constructor.java:334)中的.reflect.Constructor.newInstance0(Native Method) .BlockJUnit4ClassRunner $ 1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)atg.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)在org.junit.r unners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at org。 junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)org.junit.runners.ParentRunner.run(ParentRunner.java:363)在android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4。 java:101)org.junit.runners.Suite.runChild(Suite.java:128)org.junit.runners.Suite.runChild(Suite.java:27)at org.junit.runners.ParentRunner $ 3.run( ParentRunner.java:290)位于org.junit.runners.ParentRunner的org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)。在org访问$ 000(ParentRunner.java:58)。 junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)org.junit.runners.ParentRunner.run(ParentRunner.java:363)org.junit.runner.JUnitCore.run(JUnitCore.java:137)at org.junit.runner.JUnitCore.run(JUnitCore.java:115)位于android.support.test.runner.AndroidJUnitRunner.onStart的android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56) AndroidJUnitRunner.java:384)在android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:2075)
测试结束。
这是我在espresso设置qazxsw poi中描述的测试配置:
我认为这是因为库enter image description here与androidx
相冲突。如果你想使用jetpack,你必须将所有测试库转换为androidx,如果你不想这样,只需删除你的com.android.support.test
库并使用所有androidx
。查看我在com.android.support.test
的最新答案。希望这对你有所帮助。
我有同样的问题,并且在类中运动方法的测试之间存在差异,那些标准单位Android Instrumentation Testing: No instrumentation registered Error进入tests。与按钮,编辑文本等交互的UI app/src/test/java/<package>
需要expresso并进入tests。我花了几次阅读文档,在我弄清楚这种区别之前,我浪费了一天时间。
app/src/androidTest/java/<package>
对我有用的是彻底改变
androidX
至
com.Android.support
build.gradle中的库
请注意:您可能需要重新导入类中的库,例如
|____app
|
| ____src
| |____androidTest
| | |____java
| | |____<package>
| | |____ MainActivityInstrumentationTest.java # expresso here
| |____test
| |____java
| |____<package>
| |____ MainActivityInstrumentationTest.java # not here
至
import androidx.test.runner.AndroidJUnitRunner;
还要确保在build.gradle中的每个添加或删除库之后始终清理并重建项目。