简体   繁体   中英

Running Android JUnit test in Travis CI

I am trying to setup a continuous build system using Travis-CI. So far I manage to successfully build my Android project on Travis, but when I try to add a simple Unit test the build fails.

I get the following error on Travis: "error: package org.junit does not exist"

: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.

This is my .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

This is my Unit Test file

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);
    }
}

Can you tell me what I'm doing wrong and how to fix it?

EDIT:

Here is my build.gradle (project: Showcase)

// 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
}

This is my build.gradle (Module: 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'
}

The unit test compile and run on my computer but they do not work on Travis

Thanks

Try this if you are using emulator tests. Add to app build.gradle file in the dependencies section, as you need the library for the emulator.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM