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


    老李分享:导出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(' '));

                                         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出。有需要的朋友不理解的话,可以留言互相交流。

  • 相关阅读:
    /proc/kcore失效,调试其文件系统相关模块,使重新正常工作
    linux内核的preempt抢占调度,preempt_count抢占保护“锁”
    linux内核的tiny rcu, tree rcu
    futex-based pthread_cond 源代码分析
    linux 内核的futex
    phtread_mutex 组合
    linux 内核的rt_mutex 锁操作实现的临界区
    linux 内核的RCU本质
    Spring Data(一)概念和仓库的定义
    MongoDB之分片集群(Sharding)
  • 原文地址:https://www.cnblogs.com/poptest/p/5113664.html
Copyright © 2020-2023  润新知