我一直在尝试使用Espresso执行简单的UI测试,但我的所有测试均因相同的异常而失败:
java.lang.IllegalStateException:未注册任何工具!必须在注册工具下运行
这是使用浓缩here的初学者指南。我已经找到了类似的问题,但是与我最相关的问题没有得到解答here-我想这是因为它们没有画出全部图片,所以这是我的代码。我将仅显示一个测试,因为它们都因完全相同的错误而失败:
build.gradle(模块:应用)
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)));
}
}
我已经尝试遵循此浓缩咖啡设置文档here,但是我仍然遇到相同的错误:
开始运行测试
java.lang.IllegalStateException:未注册任何工具!必须 在注册工具下运行。在 androidx.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:50) 在 androidx.test.InstrumentationRegistry.getTargetContext(InstrumentationRegistry.java:101) 在 androidx.test.rule.ActivityTestRule。(ActivityTestRule.java:144) 在 androidx.test.rule.ActivityTestRule。(ActivityTestRule.java:120) 在 androidx.test.rule.ActivityTestRule。(ActivityTestRule.java:103) 在 io.github.vinge1718.myrestaurants.MainActivityInstrumentationTest。(MainActivityInstrumentationTest.java:25) 在java.lang.reflect.Constructor.newInstance0(本机方法)处 java.lang.reflect.Constructor.newInstance(Constructor.java:334)在 org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217) 在 org.junit.runners.BlockJUnit4ClassRunner $ 1.runReflectiveCall(BlockJUnit4ClassRunner.java:266) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在org.junit.runners.ParentRunner处$ 3.run(ParentRunner.java:290)在 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)在 org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)在 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)在 org.junit.runner.JUnitCore.run(JUnitCore.java:137)在 org.junit.runner.JUnitCore.run(JUnitCore.java:115)在 android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56) 在 android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384) 在 android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:2075)
测试完成。
这是我在浓缩咖啡设置documentation中描述的测试配置:
答案 0 :(得分:1)
我认为这是因为库androidx
与com.android.support.test
冲突。如果要使用jetpack,则必须将所有测试库都转换为androidx,如果不希望这样做,只需删除androidx
库并使用全部com.android.support.test
。在Android Instrumentation Testing: No instrumentation registered Error中查看我的最新答案。希望对您有帮助。
答案 1 :(得分:0)
我遇到了同样的问题,并且在测试中,哪些锻炼方法是课堂上的方法,那些标准单位tests进入了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
答案 2 :(得分:0)
对我有用的是完全从
改回androidX
到
com.android.support
build.gradle中的库
请注意:您可能必须重新导入类中的库,例如
import androidx.test.runner.AndroidJUnitRunner;
到
import android.support.test.runner.AndroidJUnitRunner;
还要确保在每次从build.gradle添加或删除库之后,始终清理并重建项目。