簡體   English   中英

在Travis CI中運行Android JUnit測試

[英]Running Android JUnit test in 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(模塊:應用程序)

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上運行

謝謝

如果使用仿真器測試,請嘗試此操作。 在依賴項部分中,將添加到應用程序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