我该如何构建项目的测试目录中的Andr​​oid工作室?我该、工作室、测试、项目

2023-09-04 23:50:13 作者:-苦笑 附和你的敷衍ヽ

目前对此没有约定还没有,但我怎么构建的测试目录为Android工作室,现在是怎么说的的 Android的测试基础页面不同?

There is no convention for this yet, but how do I structure the test directory for Android Studio, now that what's stated on the Android testing fundamentals page differs?

特别是,如何让JUnit测试的指针,并在Android Studio中运行将是有益的,谢谢。

Particularly, any pointers on how to get jUnit tests up and running on Android Studio would be helpful, thanks.

此外,使用机器人工具并没有真正现在帮助,因为事情是与Android Studio的有点不一样。

Also, using the android tool does not really help now, since things are a bit different with Android Studio.

更新:

我试图建立测试文件夹,并运行它,但所有我得到如下:

I tried setting up the test folder and running it, but all I'm getting is the following:

Running tests
Test running startedTest running failed: Unable to find instrumentation info for:ComponentInfo{<project-package-name>/android.test.InstrumentationTestRunner}
Empty test suite.

我也尝试添加了测试标准的Andr​​oidManifest.xml文件在那里。

I've also tried adding a standard AndroidManifest.xml file for tests in there.

推荐答案

更新

从生成工具开始的 19.1.0 并建立插件的 0.11.0 build.gradle文件需要有 testPackageName 更名为 testApplicationId (也的packageName 应该改名为 androidId )

Starting from Build Tools 19.1.0 and build plugin 0.11.0 build.gradle files needs to have testPackageName renamed to testApplicationId ( also packageName should be renamed to androidId)

作为生成插件0.9.0中的 instrumentTest 文件夹重命名为 androidTest 。这就是我们需要进行测试。

As of build plugin 0.9.0 instrumentTest folder is renamed to androidTest. That's all we need for testing.

下面是例子0.11 + DSL

Here is example of 0.11.+ DSL

android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
        androidId "org.homelab.lab"
        testApplicationId "org.homelab.lab.test"
        testInstrumentationRunner "org.homelab.lab.test.Runner"
    }

    ...
}

技巧:如果你的构建文件包括定义的 testPackageName 和 testInstrumentationRunner ,删除

GOTCHAS : if your build file consists definitions of testPackageName and testInstrumentationRunner, remove them

对于版本0.5.0 - 0.8 +

Android的Studio使用摇篮插件版本0.5。+它遵循摇篮SourceDir原则。

Android Studio uses Gradle plugin version 0.5.+ which follows Gradle SourceDir principles.

如何使其工作: 1.update SDK 2.安装或更新摇篮到1.6(报告的问题1.7)或坚持摇篮包装 3.don't采用Android工作室运行仪表任务,用摇篮命令

How to make it work: 1.update SDK 2.install or update Gradle to 1.6 (reported problems with 1.7) or stick with gradle wrapper 3.don't use Android Studio for running instrumentation task, use gradle command

gradle connectedCheck

4.don't用于测试和主要的apk 同一个包 使用浏览器5.检查结果。

4.don't use same package for test and main apk 5.check results using browser

<project>/build/reports/instrumentTests/index.html

技巧: 如果测试包和主包是一样的,可能会创建空的TestSuite。结果是误导的摇篮报告没有什么问题,但报告显示,无级已通过测试。

Gotchas: If test package and main package are the same it may create empty TestSuite. Result is misleading as Gradle reports no problems but reports show that no Class has been tested.

编辑:

下面是build.gradle的一部分配置0.9.0之前,需要仪器的测试:

Below is the part of build.gradle which configures instrument tests required before 0.9.0:

android {
    compileSdkVersion 14
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 17
        testPackageName "org.homelab.lab.test"
        testInstrumentationRunner "org.homelab.lab.test.Runner"
    }

    ...
}

示例项目https://github.com/swavkulinski/android-studio-instrumentation-test