老李分享:导出xml报告到手机

 

poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标。如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-84505200。

利用Robotium框架把报告导入手机,因为项目要求不能用Eclipse,只能用adb命令实现,可以先用开发工具生成测试工程的apk,安装到手机里,直接用命令运行,再用命令把报告pull到pc端就OK了,最好再集成到Hudson上面,就能获得更好的客户体验了,Robotium的知识不再所说,看到这篇文章的基本都懂吧,Robotium框架是不会自动生成报告到手机里的,所以要修改一下InstrumentationTestRunner类,这是单元测试的基础吧,在测试工程了新建一个InstrumentationTestRunner类,具体代码如下:

import android.os.Bundle;

import android.os.Environment;

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();

// }

// 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.和谐.test.InstrumentationTestRunner"

android:targetPackage="com.sds.android.ttpod" />

name是修改的InstrumentationTestRunner类的完整类名,targetPackage是被测试应用的包名,这是在没有源码的情况下

当然这个应用是有读写sdcard的权限的,所以上面的InstrumentationTestRunner代码可以把注释解除直接生成到sdcard里面,更加方便的adb命令pull出。有需要的朋友不理解的话,可以留言互相交流。

老李分享:导出xml报告到手机的更多相关文章

  1. POPTEST老李分享DOM解析XML之java

    POPTEST老李分享DOM解析XML之java   Java提供了两种XML解析器:树型解释器DOM(Document Object Model,文档对象模型),和流机制解析器SAX(Simple ...

  2. 老李分享:接口测试之jmeter

    老李分享:接口测试之jmeter   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.移动端自动化测试很多人把他仅仅理解成appu ...

  3. [SQLXML]FOR XML语法导出XML的易错之处

    原文:[SQLXML]FOR XML语法导出XML的易错之处 [SQLXML]FOR XML语法导出XML的易错之处 Version Date Creator Description 1.0.0.1 ...

  4. 老李分享:持续集成学好jenkins之内置命令

    老李分享:持续集成学好jenkins之内置命令   Jenkins命令调用方式:调用Jenkins命令设置job的描述信息. $JAVA_BIN-jar "$JENKINS_CLI_JAR& ...

  5. 老李分享:持续集成学好jenkins之安装

    老李分享:持续集成学好jenkins之安装   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq: ...

  6. 老李分享:持续集成学好jenkins之Git和Maven配置

    老李分享:持续集成学好jenkins之Git和Maven配置   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  7. 老李分享:webservice是什么?

    老李分享:webservice是什么?   前言 Web Services 是 Web 应用出于和其他 Web 应用以交互数据为目的的开放式标准(XML.SOAP.HTTP 等).Web Servic ...

  8. 老李分享:SSL协议相关证书

    老李分享:SSL协议相关证书   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:9088214 ...

  9. 老李分享:webservice是什么?1

    老李分享:webservice是什么?   前言 Web Services 是 Web 应用出于和其他 Web 应用以交互数据为目的的开放式标准(XML.SOAP.HTTP 等).Web Servic ...

随机推荐

  1. 使用jmeter进行APP接口测试经验总结

    声明:我觉得文章不错想保存,如果带来不便请联系我. 使用工具: Fiddler.Jmeter 测试步骤: 1.    确认接口 从开发人员那里获取接口文档,接口文档应该包括完整的功能接口.接口请求方式 ...

  2. A manager is becoming more and more popular in China

    A manager is becoming more and more popular in China; many people want to possess a position like th ...

  3. getchar()不停止原因

    应该是你的输入流中还有残留的字符,getchar()()会接受那个字符.你可以在调用getchar()()之前用fflush(stdin)刷新一下输入缓冲区. 上面一段里面,应该有读入语句吧,没读干净 ...

  4. Javascript事件绑定及深入

    由于开学后的编程实验课,接触了海量字符换搜索的实验,所以好几天没有学习JS课程了,今天继续学习事件绑定. 传统事件绑定存在一些问题,如:同名事件函数都执行,第二个函数会覆盖第一个. 下面我们以事件切换 ...

  5. 了解 : Odata 的 $filter

    api/jobPosts?$filter=company/name eq "string" //基本 api/orders?$filter=orderItem/product/EF ...

  6. 规范 : jobbox 中英文

    中英文是为了candidate 可以看到不同的post job 语言. e.g. 如果是contact person 的“designation” ,这个不会显示在post job 里,目的只是给em ...

  7. error LNK2001: unresolved external symbol __beginthreadex

    解决方法: project->settings->C++>category->code generation->Use runtime library选Debug Mul ...

  8. 关于 myBatis 中的 jdbcType的细节问题

    前几天上线了一个 版本,第二天到公司的时候,在cat上发现了一个长sql查询非常耗时,几乎把线上的项目搞崩溃了.我马上开始排查问题.最终发现 球队的ID传过来的时候是String 类型的,但是在执行s ...

  9. KoaHub平台基于Node.js开发的Koa 连接支付宝插件代码信息详情

    KoaHub平台基于Node.js开发的Koa 链接支付宝插件代码信息详情 easy-alipay alipay payment & notification APIs easy-alipay ...

  10. 某电商网站线上drbd+heartbeat+nfs配置

    1.环境 nfs1.test.com 10.1.1.1 nfs2.test.com 10.1.1.2 2.drbd配置 安装drbd yum -y install gcc gcc-c++ make g ...