gpt4 book ai didi

java - 如何使用Gradle覆盖android测试路径?

转载 作者:行者123 更新时间:2023-12-03 06:05:51 27 4
gpt4 key购买 nike

由于历史原因,检测测试未存储在androidTests目录as required中,并且我无法更改目录结构(实际上整个应用程序都是针对所用库的测试)。我能够使用gradle构建和安装apk,但未找到测试:

:app-tests:connectedDebugAndroidTest
Tests on test_avd(AVD) - 4.1.2 failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'

com.android.builder.testing.ConnectedDevice > No tests found.[test_avd(AVD) - 4.1.2] FAILED
No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack @Test annotations).
:app-tests:connectedDebugAndroidTest FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app-tests:connectedDebugAndroidTest'.
> There were failing tests. See the report at: file:///Users/asmirnov/Documents/dev/src/project/app-tests/build/reports/androidTests/connected/index.html

实际上,这些测试位于 src文件夹中,编写正确,可以使用Ant构建/运行:
public abstract class BaseTest extends AndroidTestCase
{
...

public class AppInfoTest extends BaseTest
{
@Test
public void testAllProperties()
{
...

...

我怎样才能使Gradle将 src文件夹作为android(仪器)测试进行处理?

这是我当前的 build.gradle文件(它使用 experimental Android Gradle plugin版本0.7.2,因为我们有NDK库):
apply plugin: 'com.android.model.application'

allprojects {
repositories {
mavenLocal()
mavenCentral()
}
}

repositories {
mavenLocal()
mavenCentral()
}

model {
android {
compileSdkVersion 16
buildToolsVersion "22.0.1"

defaultConfig {
applicationId "app.tests"
minSdkVersion.apiLevel 9
targetSdkVersion.apiLevel 16
versionCode 359
versionName "1.3"

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

sources {
main {
// overriding paths from default ones to actual ones
// what about 'androidTests' ?
manifest { source {
srcDir '.'
include 'AndroidManifest.xml'
} }
java { source { srcDirs = ['src'] } }
res { source { srcDirs = ['res'] } }
jni {
dependencies {
project ":library" // native library dependency
}
}
}
}
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':library')
}

更新1(用于sschuberth):

配置:
apply plugin: 'com.android.model.application'

allprojects {
repositories {
mavenLocal()
mavenCentral()
}
}

repositories {
mavenLocal()
mavenCentral()
}

model {
android {
compileSdkVersion 16
buildToolsVersion "22.0.1"

defaultConfig {
applicationId "app.tests"
minSdkVersion.apiLevel 9
targetSdkVersion.apiLevel 16
versionCode 359
versionName "1.3"

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

sourceSets {
androidTest {
manifest.srcFile 'AndroidManifest.xml' // error here
java.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':library')
}

错误
FAILURE: Build failed with an exception.

* Where:
Build file '/Users/asmirnov/Documents/dev/src/project/app-tests/build.gradle' line: 32

* What went wrong:
A problem occurred configuring project ':app-tests'.
> Exception thrown while executing model rule: android { ... } @ app-tests/build.gradle line 16, column 5
> Could not find property 'manifest' on source set 'android test'.

最佳答案

代替sources {},您应该使用sourceSets {}androidTest源集来自定义测试源代码的位置:

model { // Required for experimental plugin.
android {
sourceSets {
androidTest {
manifest.srcFile '../AppTest/AndroidManifest.xml'
java.srcDirs = ['../AppTest/src']
res.srcDirs = ['../AppTest/res']
assets.srcDirs = ['../AppTest/assets']
}
}
}
}

请注意,这不适用于单元测试。对于单元测试(例如 Robolectric),请使用 test源集。

关于java - 如何使用Gradle覆盖android测试路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38875004/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com