我正在尝试运行安卓仪器测试,似乎在模拟器上运行给出了测试结果,但在真实设备上它不起作用,输出为"测试运行失败:没有测试结果">
下面是我的build.gradle文件
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "my.app"
minSdkVersion 18
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
useLibrary 'android.test.runner'
useLibrary 'android.test.base'
useLibrary 'android.test.mock'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { assets.srcDirs = ['src/main/assets', 'src/main/assets/'] } }
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
testOptions {
unitTests.includeAndroidResources = true
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-auth:19.0.0'
implementation 'com.google.firebase:firebase-messaging:19.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.jakewharton:butterknife:10.1.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0-beta03'
implementation 'com.google.firebase:firebase-core:17.0.0'
implementation 'com.google.firebase:firebase-firestore:20.2.0'
implementation 'androidx.fragment:fragment:1.2.0-alpha02'
implementation "com.google.android.material:material:1.0.0"
implementation "com.squareup.retrofit2:retrofit:2.6.1"
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.1.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.1'
implementation 'com.github.ybq:Android-SpinKit:1.4.0'
implementation 'com.github.abdularis:circularimageview:1.4'
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
// Optional -- Robolectric environment
androidTestImplementation 'androidx.test:core:1.0.0'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
implementation "com.google.truth:truth:1.0"
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}
下面是我的简单测试用例,我从活动中找到一个按钮并将按钮文本与静态字符串进行比较
import android.widget.Button;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import salesken.app.R;
import static com.google.common.truth.Truth.assertThat;
@RunWith(AndroidJUnit4.class)
public class LoginActivityTest {
@Rule
public ActivityTestRule<LoginActivity> loginActivityTestRule = new ActivityTestRule<LoginActivity>(LoginActivity.class);
private LoginActivity loginActivity= null;
@Before
public void setUp() throws Exception {
loginActivity =loginActivityTestRule.getActivity();
}
@After
public void tearDown() throws Exception {
loginActivity = null;
}
@Test
public void testLaunch(){
Button view = loginActivity.findViewById(R.id.login);
assertThat(view.getText().toString()).isEqualTo("LOG IN");
}
}
我不明白为什么它不能在真实设备上工作。 以下是我在真实设备上运行测试时生成的日志
Testing started at 7:25 PM ...
08/25 19:25:43: Launching 'LoginActivityTest' on OnePlus ONEPLUS A5000.
Running tests
$ adb shell CLASSPATH=$(pm path androidx.test.services) app_process / androidx.test.services.shellexecutor.ShellMain am instrument -r -w -e targetInstrumentation salesken.app.test/androidx.test.runner.AndroidJUnitRunner -e debug false -e class 'salesken.app.activity.LoginActivityTest' androidx.test.orchestrator/androidx.test.orchestrator.AndroidTestOrchestrator
Waiting for process to come online...
Started running tests
Test running failed: No test results
在OnePlus上运行测试之前,您应该去
设置 ->电池 ->电池优化 -> 您的应用程序(不是 package.test but application( -> 不要优化 -> 完成
这将禁用后台限制,测试将正常工作。