我编写了一个简单的测试用例类,并将其放置在 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。
运行(顶部工具窗口)/编辑配置...
关于android - Android Studio 未找到 TestCase 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22241399/