無需root手機,得到robotium運行的測試報告

注意事項:此方法請使用命令運行,主要用於集成到hudson或者相關的自動化框架,直接運行eclipse將報錯。
運行命令:
adb shell am instrument -w com.example.android.notepad.test/com.example.android.notepad.test.InstrumentationTestRunner
得到報告:
adb pull /sdcard/TestLog D:\aaaa

用Robotium有一段時間了,下面說一下,利用這個框架把報告導入手機裏,因爲項目要求不能用Eclipse,只能用adb命令實現,可以先用開發工具生成測試工程的apk,安裝到手機裏,直接用命令運行,再用命令把報告pull到pc端就OK了,最好再集成到Hudson上面,就能獲得更好的客戶體驗了,Robotium的知識不再所說,看到這篇文章的基本都懂吧,Robotium框架是不會自動生成報告到手機裏的,所以要修改一下InstrumentationTestRunner類,這是單元測試的基礎吧,在測試工程了新建一個InstrumentationTestRunner類,具體代碼如下:
package com.example.android.notepad.test;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;

public class InstrumentationTestRunner extends
android.test.InstrumentationTestRunner {
private Writer mWriter;
private XmlSerializer mTestSuiteSerializer;
private long mTestStarted;

public void onStart() {
try {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-kk-mm");
String strTime = sdf.format(d);
String xmlName = "Test" + strTime + ".xml";
// 如果被測的應用本身有讀寫sdcard權限的話級可以直接放在sdcard裏面,否則機會失敗,
// 有測試應用源碼的情況下是可以在AndroidManifest.xml裏添加權限,當然所數情況下是沒有源碼的,
// 只能放在被測應用的files目錄裏了,這個是不需要權限的
 String SDPath = Environment.getExternalStorageDirectory() + "/";
 String logPath = SDPath + "TestLog/";
 File file = new File(logPath);
 if (file.exists()) {
 } else {
 file.mkdirs();
 }
 Log.i("qtestinadapt_cpu11111--------------------", "~~~"+file+"~~~");
 startJUnitOutput(new FileWriter(new File(file, xmlName)));
// startJUnitOutput(new FileWriter(new File(getTargetContext()
// .getFilesDir(), xmlName)));
} catch (IOException e) {
throw new RuntimeException(e);
}
super.onStart();
}

void startJUnitOutput(Writer writer) {
try {
this.mWriter = writer;
this.mTestSuiteSerializer = newSerializer(this.mWriter);
this.mTestSuiteSerializer.startDocument(null, null);
this.mTestSuiteSerializer.startTag(null, "testsuites");
this.mTestSuiteSerializer.startTag(null, "testsuite");
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private XmlSerializer newSerializer(Writer writer) {
try {
XmlPullParserFactory pf = XmlPullParserFactory.newInstance();
XmlSerializer serializer = pf.newSerializer();
serializer.setOutput(writer);
return serializer;
} catch (Exception e) {
throw new RuntimeException(e);
}

}

public void sendStatus(int resultCode, Bundle results) {
super.sendStatus(resultCode, results);
switch (resultCode) {
case -2:
case -1:
case 0:
try {
recordTestResult(resultCode, results);
} catch (IOException e) {
throw new RuntimeException(e);
}

case 1:
recordTestStart(results);
}
}

void recordTestStart(Bundle results) {
this.mTestStarted = System.currentTimeMillis();
}

void recordTestResult(int resultCode, Bundle results) throws IOException {
float time = (float) (System.currentTimeMillis() - this.mTestStarted) / 1000.0F;
String className = results.getString("class");
String testMethod = results.getString("test");
String stack = results.getString("stack");
int current = results.getInt("current");
int total = results.getInt("numtests");

this.mTestSuiteSerializer.startTag(null, "testcase");
this.mTestSuiteSerializer.attribute(null, "ID", current + "");
this.mTestSuiteSerializer.attribute(null, "classname", className);
this.mTestSuiteSerializer.attribute(null, "casename", testMethod);
// Log.v("myInfor", current + "");
if (resultCode != 0) {
this.mTestSuiteSerializer
.attribute(
null,
"time",
String.format("%.3f",
new Object[] { Float.valueOf(time) }));
this.mTestSuiteSerializer.startTag(null, "result");
if (stack != null) {
String reason = stack.substring(0, stack.indexOf('\n'));
String message = "";
int index = reason.indexOf(':');
if (index > -1) {
message = reason.substring(index + 1);
reason = reason.substring(0, index);
}
this.mTestSuiteSerializer.attribute(null, "message", message);
// this.mTestSuiteSerializer.attribute(null, "type", reason);
// this.mTestSuiteSerializer.text(stack);
this.mTestSuiteSerializer.text("failure");
}
this.mTestSuiteSerializer.endTag(null, "result");
} else {
this.mTestSuiteSerializer
.attribute(
null,
"time",
String.format("%.3f",
new Object[] { Float.valueOf(time) }));
this.mTestSuiteSerializer.startTag(null, "result");
this.mTestSuiteSerializer.attribute(null, "message", "pass");
this.mTestSuiteSerializer.text("success");
this.mTestSuiteSerializer.endTag(null, "result");
}
this.mTestSuiteSerializer.endTag(null, "testcase");
if (current == total) {
// this.mTestSuiteSerializer.startTag(null, "system-out");
// this.mTestSuiteSerializer.endTag(null, "system-out");
// this.mTestSuiteSerializer.startTag(null, "system-err");
// this.mTestSuiteSerializer.endTag(null, "system-err");
this.mTestSuiteSerializer.endTag(null, "testsuite");
this.mTestSuiteSerializer.flush();
}
}

public void finish(int resultCode, Bundle results) {
endTestSuites();
super.finish(resultCode, results);
}

void endTestSuites() {
try {
this.mTestSuiteSerializer.endTag(null, "testsuites");
this.mTestSuiteSerializer.endDocument();
this.mTestSuiteSerializer.flush();
this.mWriter.flush();
this.mWriter.close();

} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

這個類寫好之後還要在測試工程的AndroidManifest.xml裏修改一下:
    <instrumentation
        android:name="com.example.android.notepad.test.InstrumentationTestRunner"
        android:targetPackage="com.example.android.notepad" />

name是修改的InstrumentationTestRunner類的完整類名,targetPackage是被測試應用的包名,這是在沒有源碼的情況下

當然這個應用是有讀寫sdcard的權限的,所以上面的InstrumentationTestRunner代碼可以把註釋解除直接生成到sdcard裏面,更加方便的adb命令pull出。有需要的朋友不理解的話,可以留言互相交流。
發佈了2 篇原創文章 · 獲贊 0 · 訪問量 9885
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章