【问题标题】:Running Android JUnit test in Travis CI在 Travis CI 中运行 Android JUnit 测试
【发布时间】:2016-05-09 10:44:02
【问题描述】:

我正在尝试使用 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(项目: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
}

这是我的 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 上不起作用

谢谢

【问题讨论】:

  • 请发布 gradle 脚本。
  • 嗨 Nikola,我刚刚添加了我的 gradle 文件。
  • 在你的项目中寻找重复的依赖。如果你找到了,请在你的 gradle 中使用 exclude。
  • 您好 Nikola,我没有在我的项目中找到任何重复项。您是否知道在哪里可以找到可用于设置 Travis 构建系统以在 android 上运行单元测试的示例代码?

标签: android build continuous-integration travis-ci


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    相关资源
    最近更新 更多