Android单元测试:connectedAndroidTest不执行我的测试用例

时间:2015-03-23 00:58:35

标签: android unit-testing junit

我正在尝试为我的应用添加一些单元测试。我正在Android Studio中开发我的应用程序

这就是我所做的。

  • 添加了新包

  • 在新包中创建了一个扩展TestCase

  • 的类
  • 为创建的类添加了以下方法

@SmallTest public void basicTest() { assertEquals("abc", "abc"); }

  • defaultConfig
  • build.gradle部分添加了以下内容

testApplicationId "newly.added.package.name" testInstrumentationRunner "android.test.InstrumentationTestRunner"

  • 在Android Studio Terminal中执行以下命令

gradlew.bat connectedAndroidTest

但是,当我检查生成的html报告时,它显示已经运行了0个测试。

我尝试过关注,但它们都没有对gradlew.bat connectedAndroidTest命令的输出产生影响。

  • testApplicationId中向build.gradle添加了错误的包名称。
  • 已断开连接的设备

为什么我的测试用例没有被执行?我错过了什么?

以下是我的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "my.package.name"
        minSdkVersion 12
        targetSdkVersion 18
        testApplicationId "my.package.name.tests"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    lintOptions {
        abortOnError false
    }
}

dependencies {
    compile project(':my_sub_module1')
    compile project(':my_sub_module2')
    compile 'com.android.support:support-v4:21.0.2'
    compile files('libs/my_dependent_lib-1-7-4.jar')
    compile 'com.google.code.gson:gson:2.3'
}

repositories {
    mavenCentral()
}

1 个答案:

答案 0 :(得分:0)

您应该使用以下testInstrumentationRunner:

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

此外,点击Android Studio左侧的构建变体,然后选择

  

Android Instrumentation测试

在下拉列表中。

相关问题