@mSolo
2015-04-13T15:30:36.000000Z
字数 7867
阅读 2066
Jekins Android CI 持续集成 TDD

项目 build.gradle
buildscript {repositories {jcenter()}dependencies {classpath 'com.android.tools.build:gradle:0.13.2'classpath 'org.robolectric:robolectric-gradle-plugin:0.13.+'}}allprojects {repositories {jcenter()}}
App build.gradle
apply plugin: 'com.android.application'android {compileSdkVersion 18buildToolsVersion '19.1.0'defaultConfig {applicationId "com.msolo.stockeye"minSdkVersion 16targetSdkVersion 18versionCode 1versionName "1.0"}buildTypes {release {runProguard falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}packagingOptions {exclude 'META-INF/NOTICE'exclude 'META-INF/LICENSE'exclude 'META-INF/LICENSE.txt'exclude 'LICENSE.txt'}}dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])compile 'com.android.support:support-v4:19+'androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'androidTestCompile 'org.hamcrest:hamcrest-core:1.1'androidTestCompile 'org.hamcrest:hamcrest-library:1.1'androidTestCompile('junit:junit:4.+') {exclude module: 'hamcrest-core'}androidTestCompile('org.robolectric:robolectric:2.3') {exclude module: 'classworlds'exclude module: 'commons-logging'exclude module: 'httpclient'exclude module: 'maven-artifact'exclude module: 'maven-artifact-manager'exclude module: 'maven-error-diagnostics'exclude module: 'maven-model'exclude module: 'maven-project'exclude module: 'maven-settings'exclude module: 'plexus-container-default'exclude module: 'plexus-interpolation'exclude module: 'plexus-utils'exclude module: 'support-v4'exclude module: 'wagon-file'exclude module: 'wagon-http-lightweight'exclude module: 'wagon-provider-api'}}apply plugin: 'robolectric'
| build.gradle| src| |- androidTest| |- java/com/msolo/stockeye/MainActivityTest.java| |- java/com/msolo/stockeye/StockeyeApplication.java| |- main| |- java/com/msolo/stockeye/MainActivity.java| |- java/com/msolo/stockeye/StockeyeApplication.java
@RunWith(RobolectricTestRunner.class)public class MainActivityTest {@Testpublic void testActivityDisplayHelloWorldSuccessful() {String hello = new MainActivity().getResources().getString(R.string.hello_world);assertEquals("Hello World!", hello);}}
更多例子
public void testListScrolling() {listView.scrollTo(0, 0);TouchUtils.dragQuarterScreenUp(this, activity);int actualItemPosition = listView.getFirstVisiblePosition();assertTrue("Wrong position", actualItemPosition > 0);}
E:\android_workspaces\TDDStockeye>d:gradle-2.1\bin\gradle test
:app:preBuild
:app:preDebugBuild
:app:checkDebugManifest
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:compileDebugJava UP-TO-DATE
:app:compileTestDebugJava
:app:processTestDebugResources UP-TO-DATE
:app:testDebugClasses
:app:testDebug
:app:test
BUILD SUCCESSFUL
Total time: 11.647 secs
测试结果:file:///E:/android_workspaces/TDDStockeye/app/build/test-report/debug/index.html

注:TDD 快速测试(仅示范,真实项目中不必如此繁琐)
// NotificationStockQuoteTest.java@Testpublic void testSingletonOfNotificationStockQuote() {notificationStockQuote = new NotificationStockQuote();NotificationStockQuote notificationStockQuoteAnother = new NotificationStockQuote();assertNotSame(notificationStockQuoteAnother, notificationStockQuote);}// NotificationStockQuote.javapublic class NotificationStockQuote {private static NotificationStockQuote sInstance = null;private NotificationStockQuote() {}public static NotificationStockQuote getInstance() {return new NotificationStockQuote();}}
// NotificationStockQuoteTest.java@Testpublic void testSingletonOfNotificationStockQuote() {NotificationStockQuote notificationStockQuoteAnother = NotificationStockQuote.getInstance();notificationStockQuote = NotificationStockQuote.getInstance();assertSame(notificationStockQuoteAnother, notificationStockQuote);}// NotificationStockQuote.javapublic class NotificationStockQuote {private static NotificationStockQuote sInstance = null;private NotificationStockQuote() {}public static NotificationStockQuote getInstance() {return new NotificationStockQuote();}}

// NotificationStockQuoteTest.java@Beforepublic void setUp() {notificationStockQuote = NotificationStockQuote.getInstance();}@Testpublic void testSingletonOfNotificationStockQuote() {NotificationStockQuote notificationStockQuoteAnother = NotificationStockQuote.getInstance();assertSame(notificationStockQuoteAnother, notificationStockQuote);}// NotificationStockQuote.javapublic class NotificationStockQuote {private static NotificationStockQuote sInstance = null;private NotificationStockQuote() {}public static NotificationStockQuote getInstance() {if (null == sInstance) {sInstance = new NotificationStockQuote();}return sInstance;}}
// NotificationStockQuoteTest.java@Testpublic void testInit() {NotificationManager notificationManager = (NotificationManager) Robolectric.application.getSystemService(Context.NOTIFICATION_SERVICE);notificationStockQuote.init();NotificationStockQuote.HelperTestNotificationStockQuote helper = notificationStockQuote.new HelperTestNotificationStockQuote();assertNotNull(helper.getNotificationManager());assertSame(notificationManager, helper.getNotificationManager());}// NotificationStockQuote.javapublic void init() {}/**** This is an inner class for helping test NotificationStockQuote,* and it should be delete in production.** Date | Name | Change*--------+--------------------------------------------------* 141119 | mSolo | REQ #001: add method getNotificationManager().**/public class HelperTestNotificationStockQuote {public HelperTestNotificationStockQuote() {}public NotificationManager getNotificationManager() {return notificationManager;}}

@Testpublic void testInit() {NotificationManager notificationManager = (NotificationManager) Robolectric.application.getSystemService(Context.NOTIFICATION_SERVICE);//notificationStockQuote.init();MimicNotificationStockQuote mimic = new MimicNotificationStockQuote();mimic.init();assertNotNull(mimic.notificationManager);assertSame(notificationManager, mimic.notificationManager);}/**** This is an inner class for mimic NotificationStockQuote.** Date | Name | Change*--------+--------------------------------------------------* 141119 | mSolo | REQ #001: mimic method init().**/private class MimicNotificationStockQuote {public NotificationManager notificationManager = null;public MimicNotificationStockQuote() {}public void init() {notificationManager = (NotificationManager)Robolectric.application.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);}}// NotificationStockQuote.javapublic void init() {notificationManager = (NotificationManager) StockeyeApplication.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);}
