android gradle 没有运行单元测试

标签 android unit-testing android-gradle-plugin robolectric robolectric-gradle-plugin

您好,我正在尝试使用 Robolectric 测试框架在我的 Android 应用程序上使用和运行单元测试。

但是。当我运行命令 ./gradlew test 时,它似乎根本没有运行我的单元测试。

这是我得到的输出:

./gradlew test
WARNING: Dependency commons-logging:commons-logging:1.1.1 is ignored for debugAndroidTest as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.3 is ignored for debugAndroidTest as it may be conflicting with the internal version provided by Android.
         In case of problem, please repackage it with jarjar to change the class packages
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportSupportV42103Library UP-TO-DATE
:app:prepareComSothreeSlidinguppanelLibrary300Library
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources
/Users/jonathan/Work/CameraApp/app/src/main/res/drawable-mdpi/ic_drawer.png: libpng warning: iCCP: Not recognizing known sRGB profile that has been edited

/Users/jonathan/Work/CameraApp/app/src/main/res/drawable-hdpi/ic_drawer.png: libpng warning: iCCP: Not recognizing known sRGB profile that has been edited

/Users/jonathan/Work/CameraApp/app/src/main/res/drawable-xhdpi/ic_drawer.png: libpng warning: iCCP: Not recognizing known sRGB profile that has been edited

:app:processDebugManifest
:app:processDebugResources
:app:generateDebugSources
:app:compileDebugJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
:app:preCompileDebugUnitTestJava
:app:compileDebugUnitTestJava UP-TO-DATE
:app:compileDebugUnitTestSources UP-TO-DATE
:app:mockableAndroidJar
:app:assembleDebugUnitTest
:app:testDebug
:app:checkReleaseManifest
:app:prepareReleaseDependencies
:app:compileReleaseAidl
:app:compileReleaseRenderscript
:app:generateReleaseBuildConfig
:app:generateReleaseAssets UP-TO-DATE
:app:mergeReleaseAssets
:app:generateReleaseResValues
:app:generateReleaseResources
:app:mergeReleaseResources
/Users/jonathan/Work/CameraApp/app/src/main/res/drawable-mdpi/ic_drawer.png: libpng warning: iCCP: Not recognizing known sRGB profile that has been edited

/Users/jonathan/Work/CameraApp/app/src/main/res/drawable-xhdpi/ic_drawer.png: libpng warning: iCCP: Not recognizing known sRGB profile that has been edited

/Users/jonathan/Work/CameraApp/app/src/main/res/drawable-hdpi/ic_drawer.png: libpng warning: iCCP: Not recognizing known sRGB profile that has been edited

:app:processReleaseManifest
:app:processReleaseResources
:app:generateReleaseSources
:app:compileReleaseJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
:app:preCompileReleaseUnitTestJava
:app:compileReleaseUnitTestJava UP-TO-DATE
:app:compileReleaseUnitTestSources UP-TO-DATE
:app:assembleReleaseUnitTest
:app:testRelease
:app:test

BUILD SUCCESSFUL

Total time: 9.009 secs

这是我的测试类,在运行测试时应该会失败

package com.jonney.cameraapp;

import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static junit.framework.Assert.fail;

/**
 * <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
 */
@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class ApplicationTest {

    @Test
    public void testSomething() {
        fail("testing fail state");
    }
}

我的 gradle 脚本:

apply plugin: 'com.android.application'
apply plugin: 'org.robolectric'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "com.jonney.cameraapp"
        minSdkVersion 21
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:21.0.3'
    compile 'com.google.code.gson:gson:2.3'
    compile "joda-time:joda-time:2.4"
    androidTestCompile('junit:junit:4.12') {
        exclude module: 'hamcrest'
        exclude module: 'hamcrest-core'
    }
    androidTestCompile 'org.hamcrest:hamcrest-all:1.3'
    androidTestCompile('org.mockito:mockito-core:1.9.5') {
        exclude module: 'hamcrest'
        exclude module: 'hamcrest-core'
    }
    compile 'com.sothree.slidinguppanel:library:3.0.0'

    compile 'com.jakewharton:butterknife:6.1.0'

    androidTestCompile('org.robolectric:robolectric:2.4') {

    }

    compile 'com.fasterxml.jackson:jackson-parent:2.5'
}


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

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.+'
        classpath 'org.robolectric:robolectric-gradle-plugin:1.0.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

最佳答案

我想我破解了它。

我不得不将我的测试用例文件夹从 src/androidTest 重命名为 src/test

关于android gradle 没有运行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29119527/

相关文章:

android - 使用 jfeinstein10 库的滑动菜单

javascript - Jasmine 回归被 mock 工厂的 promise 被拒绝

Python "in"不检查类型?

android - 获取 java.util.zip.ZipException : duplicate entry exception

android - 在所有包变式发布任务之后执行Gradle Run自定义任务

android - 触摸点区域时扭曲图像区域?

java - 如何更新选项卡中的 ListView ?

android - 为什么在我构建项目时 Android Studio 会自动下载 Gradle?

java - 没有类定义错误android studio

c# - 关于对 Windows 窗体应用程序进行单元测试的建议