问题
I am trying to run android Instrumental Test it seem running on emulator gives test result but on real device its not working and output is "Test running failed: No test results"
Below is my build.gradle file
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'
}
Below is my simple testcase where i am finding a button from activity and comparing button text with a static string
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");
}
}
I don't understand why its not working on a real device. below is logs generated when i run the test on real device
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
回答1:
Before running tests on OnePlus you should go
Settings -> Battery -> Battery optimization -> Your app (not package.test but application) -> Don't optimize -> Done
This will disable background restrictions and tests will work fine.
来源:https://stackoverflow.com/questions/57646699/android-instrumental-test-running-on-emulator-but-not-on-real-device