我无法在我的设备上运行测试,并得到错误No instrumentation registered! Must run under a registering instrumentation.
这是我的测试类:
package com.pecode.itrustyou.ui.login;
import android.support.test.filters.MediumTest;
import android.support.test.runner.AndroidJUnit4;
import com.pecode.itrustyou.R;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.rule.ActivityTestRule;
import static androidx.test.espresso.Espresso.onView;
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.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
@RunWith(AndroidJUnit4.class)
@MediumTest
public class LoginActivityTest {
@Rule
public ActivityTestRule<LoginActivity> activityTestRule = new ActivityTestRule<>(LoginActivity.class);
@Test
public void enterInvalidPassword() {
onView(ViewMatchers.withId(R.id.etEmail)).perform(typeText("email@gmail.com"), closeSoftKeyboard());
onView(withId(R.id.etPassword)).perform(typeText("asdgfhj"), closeSoftKeyboard());
onView(withText("The username and password combination is invalid")).check(matches(isDisplayed()));
}
}
这是我的体格等级。我已经添加了所有的测试依赖项,但没有任何工作。我已经完成了这里描述的一切:在ActivityInstrumentationTestCase2中运行espresso,在默认配置中添加了AndroidJUnitRunner。
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
buildToolsVersion '28.0.3'
compileSdkVersion 28
defaultConfig {
applicationId "com.pecode.itrustyou"
minSdkVersion 21
targetSdkVersion 28
versionCode 4
versionName "0.1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
buildConfigField "String", "BASE_URL", '"https://test-itrustyouservices.azurewebsites.net"'
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "String", "BASE_URL", '"https://api.itrustyou.io"'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
android.applicationVariants.all { variant ->
variant.outputs.all {
def formattedDate = new Date().format('dd.MM.yyyy')
outputFileName = "ITrustYou-${formattedDate}.apk"
}
}
dataBinding {
enabled = true
}
}
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
///viewModel
implementation 'android.arch.lifecycle:extensions:1.1.1'
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
//recyclerView
implementation 'com.android.support:recyclerview-v7:28.0.0'
//constraint layout
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:support-v4:28.0.0'
//retrofit
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
//circleImageView
implementation 'de.hdodenhof:circleimageview:2.2.0'
///barcode
implementation(name: 'LibPdf417Mobi', ext: 'aar')
//ui
implementation 'com.aurelhubert:ahbottomnavigation:2.1.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
//eventbus
implementation 'org.greenrobot:eventbus:3.1.1'
//support
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-annotations:28.0.0'
//CrashLytics
implementation('com.crashlytics.sdk.android:crashlytics:2.9.4@aar') {
transitive = true
}
//test
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-idling-resource:3.1.0'
}
您必须在构建.gradle(应用程序(中使用ALLAndroidX依赖项
// Core library
androidTestImplementation 'androidx.test:core:1.1.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test:rules:1.1.1'
// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.1'
所以改变这条线
androidTestImplementation 'com.android.support.test:runner:1.0.2'
到这个
androidTestImplementation 'androidx.test:runner:1.1.1'
不要忘记在defaultConfig 中使用AndroidX testInstrumentationRunner
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
对我来说,问题是Android中的服务测试应该在自己的范围内进行。当你创建一个Android项目时,会在其中创建三个包:一是主体工程另一个是与前面写的服务和视图相关的Android测试(androidTest(。以及写在它前面的最后一包其他测试(测试(。
我不得不将我的测试移到androidtest文件夹中,并通过androidTestImplementation添加相关的依赖项相关的依赖关系如下:
androidTestImplementation 'androidx.test:core:(last version)'
androidTestImplementation 'androidx.test:runner:(last version)'
androidTestImplementation 'androidx.test:rules:(last version)'
androidTestImplementation 'androidx.test.espresso:espresso-core:(last version)'
androidTestImplementation 'androidx.test.espresso:espresso-intents:(last version)'