天天看点

Android自动化测试生成单元测试结果报告

  源码来自开源项目:https://code.google.com/p/nbandroid-utils/

package com.example.test.instrumentation;

import java.io.file;

import java.io.filewriter;

import java.io.ioexception;

import java.io.writer;

import org.xmlpull.v1.xmlpullparserfactory;

import org.xmlpull.v1.xmlserializer;

import android.content.context;

import android.os.bundle;

import android.os.environment;

/**

* this test runner creates a test-all.xml in the files directory of the application under test. the output is compatible with that of the junitreport ant task, the format

* that is understood by hudson. currently this implementation does not implement the all aspects of the junitreport format, but enough for hudson to parse the test results.

*/

public class instrumentationtestrunner extends android.test.instrumentationtestrunner {

private writer mwriter;

private xmlserializer mtestsuiteserializer;

private long mteststarted;

private static final string junit_xml_file = "test-all.xml";

@override

public void onstart() {

try{

file filerobo = new file(gettestresultdir(gettargetcontext()));

if(!filerobo.exists()){

filerobo.mkdir();

}

if(issdcardavaliable()){

file resultfile = new file(gettestresultdir(gettargetcontext()),junit_xml_file);

startjunitoutput(new filewriter(resultfile));

}else{

startjunitoutput(new filewriter(new file(gettargetcontext().getfilesdir(), junit_xml_file)));

catch(ioexception e){

throw new runtimeexception(e);

super.onstart();

void startjunitoutput(writer writer) {

try {

mwriter = writer;

mtestsuiteserializer = newserializer(mwriter);

mtestsuiteserializer.startdocument(null, null);

mtestsuiteserializer.starttag(null, "testsuites");

mtestsuiteserializer.starttag(null, "testsuite");

} catch (exception e) {

最新内容请见作者的github页:http://qaseven.github.io/

继续阅读