簡體   English   中英

Android Instrumental Test 在模擬器上運行但不在真實設備上

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

我正在嘗試運行 android Instrumental Test 它似乎在模擬器上運行給出了測試結果但在真實設備上它不工作並且輸出是“測試運行失敗:沒有測試結果”

下面是我的 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 而是應用程序) -> 不優化 -> 完成

這將禁用后台限制,測試將正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM