Android 自动化测试—robotium(九) Junit_report测试报告重定向输出到终端SDCard

借鉴网上相关资料主要用于无root权限的终端。主要分为以下三步:

 

一、重写InstrumentationTestRunner类: 

 

package com.exmaple.test;

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-all.xml";
            // 如果被测的应用本身有读写sdcard权限的话级可以直接放在sdcard里面,否则机会失败,
            // 有测试应用源码的情况下是可以在AndroidManifest.xml里添加权限,当然所数情况下是没有源码的,
            // 只能放在被测应用的files目录里了,这个是不需要权限的
            String SDPath = Environment.getExternalStorageDirectory() + "/";
            String logPath = SDPath + "Robotium/";
            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);
        }
    }
}

 

 

 

二、修改AndroidMainfest.xml文件为:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.exmaple.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <instrumentation
        android:name="com.exmaple.test.InstrumentationTestRunner"
        android:targetPackage="com.exmaple.my4s" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>

</manifest>

 

三、修改工程run as configuration,在instrumentation runner处,选择"com.exmaple.test.InstrumentationTestRunner",并点击apply→ok即可

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。