您现在的位置是:首页 > 文章详情

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

日期:2017-07-02点击:349
使用robotium进行 Android应用进行 自动化测试,之前用TMTS框架,但收集到的 单元测试结果常常会少掉一些用例集。。穷则思变,Android的测试框架主要是通过InstrumentationTestRunner对被测应用进行控制与执行,因此可以对InstrumentationTestRunner进行扩展以完成测试结果收集,然后通过jenkins的Publish  JUnit  test result report插件得到结果报告。
1.新建一个java package,新建一个java类
源码来自开源项目:https://code.google.com/p/nbandroid-utils/
源码中生成的TEST-all.xml结果文件位于/data/data/com.example/files目录下,要导出结果文件的话,需要 手机拥有root权限,比较麻烦,因此下面修改了文件存放路径,有SD卡则文件位于SD卡的/robotium目录下
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) {
throw new RuntimeException(e);
}
}


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

原文链接:https://yq.aliyun.com/articles/134597
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章