简体   繁体   English

Android Instrumental Test 在模拟器上运行但不在真实设备上

[英]Android Instrumental Test running on Emulator but not on real device

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"我正在尝试运行 android Instrumental Test 它似乎在模拟器上运行给出了测试结果但在真实设备上它不工作并且输出是“测试运行失败:没有测试结果”

Below is my build.gradle file下面是我的 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'


}

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

Before running tests on OnePlus you should go在 OnePlus 上运行测试之前,你应该去

Settings -> Battery -> Battery optimization -> Your app (not package.test but application) -> Don't optimize -> Done设置 -> 电池 -> 电池优化 -> 你的应用程序(不是 package.test 而是应用程序) -> 不优化 -> 完成

This will disable background restrictions and tests will work fine.这将禁用后台限制,测试将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM