android - Android Studio 未找到 TestCase 类

标签 android android-studio automated-tests

我编写了一个简单的测试用例类,并将其放置在 Android Studio 的默认测试目录中:“src/androidTest”。我创建了一个 Android 测试构建配置,用于查找模块中的所有测试。当我运行构建配置时,我的测试未执行,并且在 logcat 中收到以下消息:W/TestGrouping:无效的包:'' 无法找到或没有测试。如果我指定测试包甚至特定的测试类,我会收到类似的类未找到消息。

我的测试类如下:

public class FirstTest extends InstrumentationTestCase {

public void testSample() {
        final int expected = 1;
        final int reality = 5;
        assertEquals(expected, reality);
    }
}

我的 build.gradle 文件如下所示:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:gridlayout-v7:19.0.1'
    compile 'com.android.support:support-v4:19.0.1'
    compile 'com.android.support:appcompat-v7:19.0.1'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

我正在运行 0.9 版的 android gradle 插件。我的顶级 build.gradle (应用程序目录的同级)如下所示。

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

我的项目目录结构如下:

应用程序 源代码 android测试 java 主要的 java 资源

最佳答案

您需要检查“运行”配置。

如果您要从 androidTest 文件夹运行测试,则应选择 Android Instrumented Test 下的配置,而不是 junit。

运行(顶部工具窗口)/编辑配置...

enter image description here

关于android - Android Studio 未找到 TestCase 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22241399/

相关文章:

javascript - Selenium 构建 Internet Explorer 驱动程序 javascript 错误

reactjs - 我需要访问一个单击按钮后弹出的新窗口

android - 在 AlertDialog 中自定义 ListView

Android: RelativeLayout inside FrameLayout, width height 错误

android - 我能否判断用户是否已接受我的 Activity 的屏幕固定请求?

android - 如何在ParseFacebookUtils中使用正常的Parse函数?

java - 使用其他类(class)的 NFC Activity

android - 从 3.4.3 更新到 4.1.1 后无法检测到 OpenCV 库

android - 从 Android Studio 停止 Android adb

testing - 通过 "npm install"安装时找不到 Testcafe 命令