但不在真实设备上运行

但不在真实设备上运行

本文介绍了Android Instrument Test在模拟器上运行,但不在真实设备上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行android Instrumental Test,它似乎在模拟器上运行会给出测试结果,但在真实设备上却无法正常工作,并且输出为测试运行失败:没有测试结果"

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"

下面是我的build.gradle文件

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

推荐答案

在OnePlus上运行测试之前,您应该先

Before running tests on OnePlus you should go

这将禁用后台限制,并且测试将正常进行.

This will disable background restrictions and tests will work fine.

这篇关于Android Instrument Test在模拟器上运行,但不在真实设备上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 12:24