无需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出。有需要的朋友不理解的话,可以留言互相交流。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值