2014-03-24 88 views
1

我有以下單元測試,我可以在android studio中運行得很好,但我試圖讓它設置爲持續集成,所以我需要gradle才能運行它。Android:單元測試與gradle

package com.smartsocialmedia.tests; 

import android.test.InstrumentationTestCase; 

import com.smartsocialmedia.utility.DbConnector; 

import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONObject; 

import java.util.ArrayList; 

public class MainTest extends InstrumentationTestCase 
{ 
/* 
* Testing DbConnector 
* public JSONObject getJsonObject(ArrayList<BasicNameValuePair> params, boolean isGet) 
* pass false as second parameter if GET is to be used rather than POST 
* in this case we are calling auth/login in the API 
*/ 
public void testGETJSONOBJECT() throws Exception { 
    //Create an ArrayList of BasicNameValuePair to represent the login information. 
    ArrayList<BasicNameValuePair> postParams = new ArrayList<BasicNameValuePair>(); 
    postParams.add(new BasicNameValuePair("eMail", "myemail")); 
    postParams.add(new BasicNameValuePair("Password", "mypassword")); 
    DbConnector db = new DbConnector("auth/login"); 

    JSONObject jsonObject = db.getJsonObject(postParams, false); 

    //check that the server response is a jsonObject 
    assertNotNull(jsonObject); 
    //check that we have expected parameters in the response 
    int valid = jsonObject.getInt("valid"); 
    assertEquals(valid, 1); 
} 
} 

基本上./gradlew對此測試沒有做任何事情...我需要知道我需要在gradle中更改以使其工作。

buildscript { 
repositories { 
    mavenCentral() 
} 
    dependencies { 
    classpath 'com.android.tools.build:gradle:0.9.+' 
    } 
} 
apply plugin: 'android' 

repositories { 
    mavenCentral() 
} 

android { 
    compileSdkVersion 19 
    buildToolsVersion '19.0.0' 

    lintOptions { 
     checkReleaseBuilds false 
     // Or, if you prefer, you can continue to check for errors in release builds, 
     // but continue the build even when errors are found: 
     abortOnError false 
    } 

defaultConfig { 
    minSdkVersion 14 
    targetSdkVersion 19 
    versionCode 7 
    versionName "1.0.5" 
    testPackageName "com.smartsocialmedia.tests" 
} 

buildTypes { 
    release { 
     runProguard false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' 
     } 
    } 
} 

dependencies { 
    compile 'com.android.support:support-v4:18.0.0' 
    compile 'org.apache.httpcomponents:httpmime:[email protected]' 
    compile 'com.github.chrisbanes.actionbarpulltorefresh:library:+' 
    compile files('libs/universal-image-loader-1.9.1-with-sources.jar') 
} 
+0

你試圖gradlew connectedAndroidTest? – Mick

+0

你正在運行什麼gradle任務?如果一切正常,connectedCheck將在/ androidTest /中運行測試。 –

+0

@ ed209你解決了嗎? –

回答

0

你需要有至少1.1.0-RC1(2015年2月2日)的Android插件的gradle

單元測試支持。單元測試代碼在本地JVM上運行,與特定版本的android.jar兼容,這些代碼與流行的嘲笑框架(例如Mockito)兼容。

http://tools.android.com/tech-docs/new-build-system

+0

這僅僅適用於在本地JVM上運行的單元測試。 InstrumentationTestCase在設備上運行。 –

相關問題