简体   繁体   English

在Travis CI中运行Android JUnit测试

[英]Running Android JUnit test in Travis CI

I am trying to setup a continuous build system using Travis-CI. 我正在尝试使用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. 到目前为止,我设法在Travis上成功构建了Android项目,但是当我尝试添加简单的单元测试时,构建失败。

I get the following error on Travis: "error: package org.junit does not exist" 我在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.

This is my .travis.yml 这是我的.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) 这是我的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
}

This is my build.gradle (Module: app) 这是我的build.gradle(模块:应用程序)

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 单元测试可以在我的计算机上编译并运行,但不能在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. 在依赖项部分中,将添加到应用程序build.gradle文件,因为您需要模拟器库。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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