Robotium [求助] 在 AndroidStudio 中使用 Robotium 出错,找不到测试 class

剪烛 · 2017年07月05日 · 最后由 晨晨 回复于 2021年06月26日 · 3625 次阅读

orz 初衷很简单,本是想在写个自动化脚本用 intent 直接调用特定的 activity,因为权限问题,只能用 Robotium 这类的框架,但是后来发现用在 Android Studio 中用 Robotium 简直到处碰壁。求救。

测试类是:com.aabbcc.robtest.ExampleInstrumentedTest
测试目标 apk:com.aabbcc.client.stg
测试脚本所在的包名:com.aabbcc.robtest

工程的结构如下:

manifests 设置

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.aabbcc.robtest">

    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.aabbcc.client.stg">

    </instrumentation>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

    </application>

</manifest>

gradle 设置:

apply plugin: 'com.android.application'

android {
    signingConfigs {
        config {
            keyAlias 'androiddebugkey'
            keyPassword 'android'
            storeFile file('/Users/zanghuizhi/Downloads/tttttt/android.keystore')
            storePassword 'android'
        }
    }
    compileSdkVersion 25
    buildToolsVersion '25.0.0'
    defaultConfig {
        applicationId "com.aabbcc.robtest"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            signingConfig signingConfigs.config
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.1', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.+'
    testCompile 'junit:junit:4.12'
    androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.3.1'
}

测试代码

package com.aabbcc.robtest;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;

import com.robotium.solo.Solo;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */

public class ExampleInstrumentedTest extends ActivityInstrumentationTestCase2 {
    private Solo solo;
    private static String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.aabbcc.client.module.mologin.activity.SplashActivity";
    private static String TARGET_PACKAGE_ID = "com.aabbcc.client.stg";
    public ExampleInstrumentedTest() throws ClassNotFoundException {
        super(TARGET_PACKAGE_ID, Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME));
    }

    @Before
    public void setUp() throws Exception {
        solo = new Solo(this.getInstrumentation(), this.getActivity());

    }

    @Test
    public void useAppContext() throws Exception {
        solo.sleep(5000);
        solo.clickLongOnText("生日问候");
        solo.sleep(5000);
    }

    @After
    public void tearDown() throws Exception {

        solo.finishOpenedActivities();
    }
}

直接在 AndroidStudio 中直接运行测试,会发现很诡异的用的是 AndroidJUnitRunner,然后找不到 instrumentation info

07/05 17:42:02: Launching ExampleInstrumentedT...
$ adb push /Users/zanghuizhi/AndroidStudioProjects/RobTest/app/build/outputs/apk/app-debug.apk /data/local/tmp/com.aabbcc.robtest
$ adb shell pm install -r "/data/local/tmp/com.aabbcc.robtest"
Success


$ adb push /Users/zanghuizhi/AndroidStudioProjects/RobTest/app/build/outputs/apk/app-debug-androidTest.apk /data/local/tmp/com.aabbcc.robtest.test
$ adb shell pm install -r "/data/local/tmp/com.aabbcc.robtest.test"
Success


Running tests

$ adb shell am instrument -w -r   -e debug false -e class com.aabbcc.robtest.ExampleInstrumentedTest com.aabbcc.robtest.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
Test running failed: Unable to find instrumentation info for: ComponentInfo{com.aabbcc.robtest.test/android.support.test.runner.AndroidJUnitRunner}
Empty test suite.

用命令执行测试,出现 process crashed

 adb shell am instrument -w -r   -e debug false -e class com.aabbcc.robtest.ExampleInstrumentedTest com.aabbcc.robtest/android.test.InstrumentationTestRunner
INSTRUMENTATION_RESULT: shortMsg=Process crashed.
INSTRUMENTATION_CODE: 0

看 logcat 的日志发现是

07-05 17:05:24.139 16586 16586 E AndroidRuntime: FATAL EXCEPTION: main
07-05 17:05:24.139 16586 16586 E AndroidRuntime: Process: com.aabbcc.client.stg, PID: 16586
07-05 17:05:24.139 16586 16586 E AndroidRuntime: java.lang.RuntimeException: Exception thrown in onCreate() of ComponentInfo{com.aabbcc.robtest/android.test.InstrumentationTestRunner}: java.lang.RuntimeException: Could not find test class. Class: com.aabbcc.robtest.ExampleInstrumentedTest
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5427)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at android.app.ActivityThread.-wrap2(ActivityThread.java)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1568)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:102)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:160)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:6200)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
07-05 17:05:24.139 16586 16586 E AndroidRuntime: Caused by: java.lang.RuntimeException: Could not find test class. Class: com.aabbcc.robtest.ExampleInstrumentedTest
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at android.test.AndroidTestRunner.runFailed(AndroidTestRunner.java:262)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at android.test.AndroidTestRunner.loadTestClass(AndroidTestRunner.java:96)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:57)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at android.test.suitebuilder.TestSuiteBuilder.addTestClassByName(TestSuiteBuilder.java:78)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at android.test.InstrumentationTestRunner.parseTestClass(InstrumentationTestRunner.java:449)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at android.test.InstrumentationTestRunner.parseTestClasses(InstrumentationTestRunner.java:430)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:376)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5424)
07-05 17:05:24.139 16586 16586 E AndroidRuntime:    ... 8 more
07-05 17:05:24.164 16586 16586 I Process : Sending signal. PID: 16586 SIG: 9

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 10 条回复 时间 点赞

不需要继承 ActivityInstrumentationTestCase2 吧?

pm list instrumentation 可以列出已安装的测试包

testInstrumentationRunner "android.test.InstrumentationTestRunner" 改成 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

恒温 回复

好几年不用了,感觉一脸懵逼。😂,我是不是得再看下官方文档

bauul 回复

我看了,是有的,但是调用就找不到 class😂

执行使用的 testrunner 和 在 gradle 中设置的 testrunner 不是同一个, 你用的是 Android Studio 2.3 + 吧

执行这个命令, 看能执行么 。 adb shell am instrument -w -r -e debug false -e class com.aabbcc.robtest.ExampleInstrumentedTest com.aabbcc.robtest.test/android.test.InstrumentationTestRunner

楼主问题解决了吗 ,我碰到跟你一样的问题

晨晨 回复

四年前……记忆模糊了……但是现在基本很少有人用 Robotium 了吧

10楼 已删除
剪烛 回复

Robotium 加 uiautomator 配合使用 不过这代码也是好多年前写的

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册