Android Studio 未找到 TestCase 类 [英] TestCase class not found by Android Studio
问题描述
我编写了一个简单的测试用例类,并将其放置在 Android Studio 的默认测试目录中:src/androidTest".我创建了一个 Android 测试构建配置,用于查找模块中的所有测试.当我运行构建配置时,我的测试没有执行,我在 logcat 中收到以下消息:W/TestGrouping:无效包:'' 找不到或没有测试
.如果我指定测试包甚至特定的测试类,我会收到类似的未找到类的消息.
I have written a simple test case class and placed it in the default test directory for Android Studio: "src/androidTest". I've created an Android Tests build configuration that looks for all Tests in the module. When I run the build configuration, my test does not execute and I get the following message in logcat: W/TestGrouping﹕ Invalid Package: '' could not be found or has no tests
. If I specify the test package or even the specific test class, I get similar class not found messages.
我的测试类如下:
public class FirstTest extends InstrumentationTestCase {
public void testSample() {
final int expected = 1;
final int reality = 5;
assertEquals(expected, reality);
}
}
我的 build.gradle 文件如下所示:
My build.gradle file looks like this:
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(应用程序目录的对等点)看起来像这样.
I'm running version 0.9 of the android gradle plugin. My top level build.gradle (a peer to the app directory) looks like this.
// 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()
}
}
我的项目目录结构如下:
My project directory structure is as follows:
<代码>应用源文件安卓测试爪哇主要的爪哇资源
推荐答案
androidTest/java目录下的包结构需要与main/java目录下的结构完全平行.
The package structure under the androidTest/java directory needs to exactly parallel the structure under the main/java directory.
我上面的问题是main下的包结构是com.mydomain.myapp.subpackage
,androidTest下的目录结构是com.mydomain.myapp.subpackage.somethingelse
.
My problem above was that the package structure under main was com.mydomain.myapp.subpackage
and the directory structure under androidTest was com.mydomain.myapp.subpackage.somethingelse
.
一旦包结构匹配,测试就会被发现并完美执行.
Once the package structures matched, the tests were discovered and executed flawlessly.
这篇关于Android Studio 未找到 TestCase 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!