原文來自:http://blog.csdn.net/hunterno4/article/details/14485663
並補充對改代碼的優化
代碼部分
public class SampleTestResult extends InstrumentationTestRunner {
private static final String JUNIT_XML_FILE = "TEST-all.xml";
private XmlSerializer mTestSuiteSerializer;
private Writer mWriter;
private long mTestStarted;
@Override
public TestSuite getAllTests() {
return new TestSuite();
}
/***
* 開始測試時調用此方法
*/
@Override
public void onStart() {
try {
File fileRobo = new File(getTestResultDir(getTargetContext()));
if (!fileRobo.exists()) {
fileRobo.mkdir();
}
if (isSDCardAvaliable()) {
File resultFile = new File(
getTestResultDir(getTargetContext()), JUNIT_XML_FILE);
startJUnitOutput(new FileWriter(resultFile));
} else {
startJUnitOutput(new FileWriter(new File(getTargetContext()
.getFilesDir(), JUNIT_XML_FILE)));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
super.onStart();
}
/**
* 創建寫文件流內容
*
* @param writer
*/
void startJUnitOutput(Writer writer) {
try {
mWriter = writer;
mTestSuiteSerializer = newSerializer(mWriter);
mTestSuiteSerializer.startDocument(null, null);
mTestSuiteSerializer.startTag(null, "testsuites");
mTestSuiteSerializer.startTag(null, "testsuite");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 獲取測試結果報告文件所在的路徑
*
* @param context
* 被測工程的context
* @return 返回測試結果報告文件所在的路徑
*/
private String getTestResultDir(Context context) {
String packageName = "/" + "robotium";
String filepath = context.getCacheDir().getPath() + packageName;
if (android.os.Build.VERSION.SDK_INT < 8) {
if (isSDCardAvaliable()) {
filepath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + packageName;
}
} else {
if (isSDCardAvaliable()) {
filepath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + packageName;
}
}
return filepath;
}
/***
* 創建寫文件方法
*
* @param writer
* @return
*/
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);
}
}
/**
* 判斷SD卡是否存在
*
* @return
*/
private boolean isSDCardAvaliable() {
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
}
@Override
public void sendStatus(int resultCode, Bundle results) {
super.sendStatus(resultCode, results);
switch (resultCode) {
case REPORT_VALUE_RESULT_ERROR: // 測試完成-測試錯誤
case REPORT_VALUE_RESULT_FAILURE:// 測試完成-測試失效
case REPORT_VALUE_RESULT_OK:// 測試完成-測試成功
try {
recordTestResult(resultCode, results);
} catch (IOException e) {
throw new RuntimeException(e);
}
break;
case REPORT_VALUE_RESULT_START:// 開始測試
recordTestStart(results);
default:
break;
}
}
/***
* 開始測試時間
*
* @param results
*/
void recordTestStart(Bundle results) {
mTestStarted = System.currentTimeMillis();
}
/***
* 測試結果存放如xml樣式
*
* @param resultCode
* @param results
* 測試結果傳過來的數據
* @throws IOException
*/
void recordTestResult(int resultCode, Bundle results) throws IOException {
float time = (System.currentTimeMillis() - mTestStarted) / 1000.0f;// 獲取系統測試時間
String className = results.getString(REPORT_KEY_NAME_CLASS);// 測試類名稱
String testMethod = results.getString(REPORT_KEY_NAME_TEST);// 測試方法
String stack = results.getString(REPORT_KEY_STACK);// 標識一個堆棧跟蹤描述錯誤或失敗。這是發送任何狀態信息描述一個特定的測試完成。
int current = results.getInt(REPORT_KEY_NUM_CURRENT);// 測試序列號
int total = results.getInt(REPORT_KEY_NUM_TOTAL);// 正在運行測試的總數
mTestSuiteSerializer.startTag(null, "testcase");
mTestSuiteSerializer.attribute(null, "classname", className);
mTestSuiteSerializer.attribute(null, "name", testMethod);
if (resultCode != REPORT_VALUE_RESULT_OK) {
mTestSuiteSerializer.startTag(null, "failure");
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);
}
mTestSuiteSerializer.attribute(null, "message", message);
mTestSuiteSerializer.attribute(null, "type", reason);
mTestSuiteSerializer.text(stack);
}
mTestSuiteSerializer.endTag(null, "failure");
} else {
mTestSuiteSerializer.attribute(null, "time",
String.format("%.3f", time));
}
mTestSuiteSerializer.endTag(null, "testcase");
if (current == total) {
mTestSuiteSerializer.startTag(null, "system-out");
mTestSuiteSerializer.endTag(null, "system-out");
mTestSuiteSerializer.startTag(null, "system-err");
mTestSuiteSerializer.endTag(null, "system-err");
mTestSuiteSerializer.endTag(null, "testsuite");
mTestSuiteSerializer.flush();
}
}
/***
* 測試結束後調用此方法
*/
@Override
public void finish(int resultCode, Bundle results) {
endTestSuites();
super.finish(resultCode, results);
}
/***
* 創建測試結束文件內容
*/
void endTestSuites() {
try {
mTestSuiteSerializer.endTag(null, "testsuites");
mTestSuiteSerializer.endDocument();
mTestSuiteSerializer.flush();
mWriter.flush();
mWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
2.在Androidmainfest.xml文件中需要進行修改:
原來代碼顯示爲:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.被測app包名.cn" />
修改後的代碼顯示爲:<instrumentation android:name="com.targtime.mtll.testResult.SampleTestResult"
android:targetPackage="com.被測app包名.cn" />
3.編寫完成後,需要在eclipse中右鍵運行時選擇“run configurations”-junit-中的需要運行的測試類名,並在test中設置“run時選用的
InstrumentationTestRunner爲最新創建的“SampleTestResult類”
4.運行後進入手機SD卡中超找文件,並導出本地結果進行查看
補充:
5.因爲多個類可以需要同時運行,所以需要建立一個“TestSuite”類
public class SampleTestSuite extends TestSuite {
public static Test suite() {
TestSuite ts = new TestSuite();
ts.addTestSuite(Test1.class);
ts.addTestSuite(Test2.class);
return ts;
}
}
直接運行此類所有測試類的結果都在文件中生成