Robotium自动化测试利用InstrumentationTestRunner和XmlSerializer导出xml报告到手机里

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

[java]   view plain copy
  1. import android.os.Bundle;  
  2. import android.os.Environment;  
  3. import java.io.File;  
  4. import java.io.FileWriter;  
  5. import java.io.IOException;  
  6. import java.io.Writer;  
  7. import java.text.SimpleDateFormat;  
  8. import java.util.Date;  
  9.   
  10. import org.xmlpull.v1.XmlPullParserFactory;  
  11. import org.xmlpull.v1.XmlSerializer;  
  12.   
  13. public class InstrumentationTestRunner extends  
  14.         android.test.InstrumentationTestRunner {  
  15.     private Writer mWriter;  
  16.     private XmlSerializer mTestSuiteSerializer;  
  17.     private long mTestStarted;  
  18.   
  19.     public void onStart() {  
  20.         try {  
  21.             Date d = new Date();  
  22.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-kk-mm");  
  23.             String strTime = sdf.format(d);  
  24.             String xmlName = "Test" + strTime + ".xml";  
  25.             // 如果被测的应用本身有读写sdcard权限的话级可以直接放在sdcard里面,否则机会失败,  
  26.             // 有测试应用源码的情况下是可以在AndroidManifest.xml里添加权限,当然所数情况下是没有源码的,  
  27.             // 只能放在被测应用的files目录里了,这个是不需要权限的  
  28.             // String SDPath = Environment.getExternalStorageDirectory() + "/";  
  29.             // String logPath = SDPath + "TestLog/";  
  30.             // File file = new File(logPath);  
  31.             // if (file.exists()) {  
  32.             // } else {  
  33.             // file.mkdirs();  
  34.             // }  
  35.             // startJUnitOutput(new FileWriter(new File(file, xmlName)));  
  36.             startJUnitOutput(new FileWriter(new File(getTargetContext()  
  37.                     .getFilesDir(), xmlName)));  
  38.         } catch (IOException e) {  
  39.             throw new RuntimeException(e);  
  40.         }  
  41.         super.onStart();  
  42.     }  
  43.   
  44.     void startJUnitOutput(Writer writer) {  
  45.         try {  
  46.             this.mWriter = writer;  
  47.             this.mTestSuiteSerializer = newSerializer(this.mWriter);  
  48.             this.mTestSuiteSerializer.startDocument(nullnull);  
  49.             this.mTestSuiteSerializer.startTag(null"testsuites");  
  50.             this.mTestSuiteSerializer.startTag(null"testsuite");  
  51.         } catch (Exception e) {  
  52.             throw new RuntimeException(e);  
  53.         }  
  54.     }  
  55.   
  56.     private XmlSerializer newSerializer(Writer writer) {  
  57.         try {  
  58.             XmlPullParserFactory pf = XmlPullParserFactory.newInstance();  
  59.             XmlSerializer serializer = pf.newSerializer();  
  60.             serializer.setOutput(writer);  
  61.             return serializer;  
  62.         } catch (Exception e) {  
  63.             throw new RuntimeException(e);  
  64.         }  
  65.   
  66.     }  
  67.   
  68.     public void sendStatus(int resultCode, Bundle results) {  
  69.         super.sendStatus(resultCode, results);  
  70.         switch (resultCode) {  
  71.         case -2:  
  72.         case -1:  
  73.         case 0:  
  74.             try {  
  75.                 recordTestResult(resultCode, results);  
  76.             } catch (IOException e) {  
  77.                 throw new RuntimeException(e);  
  78.             }  
  79.   
  80.         case 1:  
  81.             recordTestStart(results);  
  82.         }  
  83.     }  
  84.   
  85.     void recordTestStart(Bundle results) {  
  86.         this.mTestStarted = System.currentTimeMillis();  
  87.     }  
  88.   
  89.     void recordTestResult(int resultCode, Bundle results) throws IOException {  
  90.         float time = (float) (System.currentTimeMillis() - this.mTestStarted) / 1000.0F;  
  91.         String className = results.getString("class");  
  92.         String testMethod = results.getString("test");  
  93.         String stack = results.getString("stack");  
  94.         int current = results.getInt("current");  
  95.         int total = results.getInt("numtests");  
  96.   
  97.         this.mTestSuiteSerializer.startTag(null"testcase");  
  98.         this.mTestSuiteSerializer.attribute(null"ID", current + "");  
  99.         this.mTestSuiteSerializer.attribute(null"classname", className);  
  100.         this.mTestSuiteSerializer.attribute(null"casename", testMethod);  
  101.         // Log.v("myInfor", current + "");  
  102.         if (resultCode != 0) {  
  103.             this.mTestSuiteSerializer  
  104.                     .attribute(  
  105.                             null,  
  106.                             "time",  
  107.                             String.format("%.3f",  
  108.                                     new Object[] { Float.valueOf(time) }));  
  109.             this.mTestSuiteSerializer.startTag(null"result");  
  110.             if (stack != null) {  
  111.                 String reason = stack.substring(0, stack.indexOf('\n'));  
  112.                 String message = "";  
  113.                 int index = reason.indexOf(':');  
  114.                 if (index > -1) {  
  115.                     message = reason.substring(index + 1);  
  116.                     reason = reason.substring(0, index);  
  117.                 }  
  118.                 this.mTestSuiteSerializer.attribute(null"message", message);  
  119.                 // this.mTestSuiteSerializer.attribute(null, "type", reason);  
  120.                 // this.mTestSuiteSerializer.text(stack);  
  121.                 this.mTestSuiteSerializer.text("failure");  
  122.             }  
  123.             this.mTestSuiteSerializer.endTag(null"result");  
  124.         } else {  
  125.             this.mTestSuiteSerializer  
  126.                     .attribute(  
  127.                             null,  
  128.                             "time",  
  129.                             String.format("%.3f",  
  130.                                     new Object[] { Float.valueOf(time) }));  
  131.             this.mTestSuiteSerializer.startTag(null"result");  
  132.             this.mTestSuiteSerializer.attribute(null"message""pass");  
  133.             this.mTestSuiteSerializer.text("success");  
  134.             this.mTestSuiteSerializer.endTag(null"result");  
  135.         }  
  136.         this.mTestSuiteSerializer.endTag(null"testcase");  
  137.         if (current == total) {  
  138.             // this.mTestSuiteSerializer.startTag(null, "system-out");  
  139.             // this.mTestSuiteSerializer.endTag(null, "system-out");  
  140.             // this.mTestSuiteSerializer.startTag(null, "system-err");  
  141.             // this.mTestSuiteSerializer.endTag(null, "system-err");  
  142.             this.mTestSuiteSerializer.endTag(null"testsuite");  
  143.             this.mTestSuiteSerializer.flush();  
  144.         }  
  145.     }  
  146.   
  147.     public void finish(int resultCode, Bundle results) {  
  148.         endTestSuites();  
  149.         super.finish(resultCode, results);  
  150.     }  
  151.   
  152.     void endTestSuites() {  
  153.         try {  
  154.             this.mTestSuiteSerializer.endTag(null"testsuites");  
  155.             this.mTestSuiteSerializer.endDocument();  
  156.             this.mTestSuiteSerializer.flush();  
  157.             this.mWriter.flush();  
  158.             this.mWriter.close();  
  159.   
  160.         } catch (IOException e) {  
  161.             throw new RuntimeException(e);  
  162.         }  
  163.     }  
  164. }  

 

这个类写好之后还要在测试工程的AndroidManifest.xml里修改一下:

[html]   view plain copy
  1. <instrumentation  
  2.         android:name="com.和谐.test.InstrumentationTestRunner"  
  3.         android:targetPackage="com.sds.android.ttpod" />  


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

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

 

转载:http://blog.csdn.net/onepiece2345/article/details/8165241

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值