在Travis CI中运行Android JUnit测试

时间:2016-02-01 03:56:03

标签: android build continuous-integration travis-ci

我正在尝试使用Travis-CI设置连续构建系统。到目前为止,我设法在Travis上成功构建我的Android项目,但是当我尝试添加一个简单的单元测试时,构建失败了。

我在Travis上收到以下错误:“错误:包org.junit不存在”

:app:compileDebugAndroidTestJavaWithJavac/home/travis/build/Namguz/Showcase/app/src/androidTest/java/showcase/showcase/SearchableActivityTest.java:5: error: package org.junit does not exist
import org.junit.Test;
            ^
/home/travis/build/Namguz/Showcase/app/src/androidTest/java/showcase/showcase/SearchableActivityTest.java:6: error: package org.junit does not exist
import org.junit.Before;
            ^
/home/travis/build/Namguz/Showcase/app/src/androidTest/java/showcase/showcase/SearchableActivityTest.java:7: error: package org.junit does not exist
import static org.junit.Assert.*;
                   ^
/home/travis/build/Namguz/Showcase/app/src/androidTest/java/showcase/showcase/SearchableActivityTest.java:12: error: cannot find symbol
@Before
 ^
symbol:   class Before
location: class SearchableActivityTest
/home/travis/build/Namguz/Showcase/app/src/androidTest/java/showcase/showcase/SearchableActivityTest.java:18: error: cannot find symbol
@Test
 ^
symbol:   class Test
location: class SearchableActivityTest
/home/travis/build/Namguz/Showcase/app/src/androidTest/java/showcase/showcase/SearchableActivityTest.java:22: error: cannot find symbol
    assertFalse("Dummy function should return true", result);
    ^
symbol:   method assertFalse(String,boolean)
location: class SearchableActivityTest
6 errors
FAILED
FAILURE: Build failed with an exception.

这是我的.travis.yml

language: android
android:
components:
- platform-tools
- tools

# The BuildTools version used by your project
- build-tools-23.0.2

# The SDK version used to compile your project
- android-23

# Additional components
- extra-google-google_play_services
- extra-google-m2repository
- extra-android-m2repository

# Specify at least one system image,
# if you need to run emulator(s) during your tests
# - sys-img-armeabi-v7a-android-21
- sys-img-armeabi-v7a-android-23

before_install:
- sudo chmod +x gradlew

env:
 global:
 # install timeout in minutes (2 minutes by default)
- ADB_INSTALL_TIMEOUT=8

# Emulator Management: Create, Start and Wait
before_script:
- echo no | android create avd --force --name test --target android-23 --abi armeabi-v7a
- emulator -avd test -no-skin -no-audio -no-window -gpu off -no-boot-anim &
- android-wait-for-emulator
- adb devices
- adb shell input keyevent 82 &

script:
- echo $ADB_INSTALL_TIMEOUT
- android list target
- ./gradlew clean
- ./gradlew assembleDebug
- ./gradlew assembleDebugAndroidTest

这是我的单元测试文件

package showcase.showcase;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.Before;
import static org.junit.Assert.*;

public class SearchableActivityTest
{
    SearchableActivity searchableActivity;

    @Before
    public void setUp() throws Exception
    {
        searchableActivity = new SearchableActivity();
    }

    @Test
    public void searchQuesryTest() throws  Exception
    {
        boolean result = searchableActivity.dummyTest();
        assertTrue("Dummy function should return true", result);
    }
}

你能告诉我我做错了什么以及如何解决它?

修改

这是我的build.gradle(项目:展示)

// 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.5.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这是我的build.gradle(模块:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
}

单元测试在我的计算机上编译并运行,但它们不适用于Travis

由于

1 个答案:

答案 0 :(得分:0)

如果您正在使用模拟器测试,请尝试此操作。 添加到依赖项部分中的app build.gradle文件,因为您需要模拟器的库。

// android emulator test dependencies
androidTestCompile 'junit:junit:4.12'