简体   繁体   English

Android Studio中的Robolectric和Instrumentation测试

[英]Robolectric and Instrumentation tests in Android Studio

I want to create Robolectric tests to supplement my existing instrumentation tests. 我想创建Robolectric测试以补充现有的仪器测试。 I have found a gradle plugin that purports to support a separate module with the Robolectric tests. 我发现了gradle插件 ,该插件声称支持Robolectric测试的单独模块。 However, when I attempt to use this plugin, I get the following error: 但是,当我尝试使用此插件时,出现以下错误:

Class not found: "bbct.android.test.activity.test.FilterCardsTest" 找不到类别:“ bbct.android.test.activity.test.FilterCardsTest”

Here is my build.gradle file: 这是我的build.gradle文件:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"

    defaultConfig {
        applicationId "bbct.android.common"
        minSdkVersion 11
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    signingConfigs {
        release
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
        lite {
            applicationId "bbct.android"
            versionCode 15
            versionName "0.6.2"
        }
        premium {
            applicationId "bbct.android.premium"
            versionCode 14
            versionName "0.6.2"
        }
    }
    def Properties props = new Properties()
    def propFile = new File('signing.properties')
    if (propFile.canRead()) {
        props.load(new FileInputStream(propFile))

        if (props!=null && props.containsKey('STORE_FILE') && props.containsKey('STORE_PASSWORD') &&
                props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD')) {
            android.signingConfigs.release.storeFile = file(props['STORE_FILE'])
            android.signingConfigs.release.storePassword = props['STORE_PASSWORD']
            android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
            android.signingConfigs.release.keyPassword = props['KEY_PASSWORD']
        } else {
            println 'signing.properties found but some entries are missing'
            android.buildTypes.release.signingConfig = null
        }
    } else {
        println 'signing.properties not found'
        android.buildTypes.release.signingConfig = null
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.1.0'
    compile 'com.android.support:support-v4:19.1.0'
    compile 'com.google.android.gms:play-services:4.2.42'
    liteCompile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.1'
    androidTestCompile ('com.squareup:fest-android:1.0.8') {
        exclude group: 'com.android.support'
    }
}

And FilterCardsTest : FilterCardsTest

package bbct.android.test.activity.test;

import android.app.Activity;
import bbct.android.common.activity.FragmentTestActivity;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static org.junit.Assert.assertTrue;

@Config(manifest = "./src/main/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
public class FilterCardsTest {

    @Test
    public void testSomething() throws Exception {
        Activity activity = Robolectric.buildActivity(FragmentTestActivity.class).create().get();
        assertTrue(activity != null);
    }

}

Where get you the class not found exception? 哪里找不到类异常? I'm expecting it comes when you robolectric test inside Android studio. 我希望当您在Android Studio中进行自动测试时会出现这种情况。

There may be different reason. 可能有不同的原因。 First your test class will not be compiled when you start a test in android studio. 首先,当您在android studio中开始测试时,不会编译您的测试类。 Second Android studio doesn't know where to search for the compiled class. Second Android Studio不知道在哪里搜索编译的类。

For first issue just compile your test classes. 对于第一个问题,只需编译您的测试类。 For the second issue you can modify the test module iml file where you must change the path of the test-output to your needs. 对于第二个问题,您可以修改测试模块iml文件,在该文件中您必须根据需要更改测试输出的路径。

Here is a full example, which will do the iml modification automatically. 这是一个完整的示例,它将自动进行iml修改。 https://github.com/nenick/android-gradle-template https://github.com/nenick/android-gradle-template

And here details how you can force android studio to compile your test classes https://github.com/nenick/android-gradle-template/wiki/Tests-in-Android-Studio---IntellJ 这里详细说明了如何强制android studio编译测试类https://github.com/nenick/android-gradle-template/wiki/Tests-in-Android-Studio---IntellJ

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

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