您的位置:首页 > 其它

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

2012-11-09 11:04 429 查看
用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出。有需要的朋友不理解的话,可以留言互相交流。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: