qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          Android自動化測試生成單元測試結(jié)果報告

            使用robotium進行Android應(yīng)用進行自動化測試,之前用TMTS框架,但收集到的單元測試結(jié)果常常會少掉一些用例集。。窮則思變,Android的測試框架主要是通過InstrumentationTestRunner對被測應(yīng)用進行控制與執(zhí)行,因此可以對InstrumentationTestRunner進行擴展以完成測試結(jié)果收集,然后通過jenkins的Publish JUnit test result report插件得到結(jié)果報告。
            1.新建一個java package,新建一個java類
            源碼來自開源項目:https://code.google.com/p/nbandroid-utils/
            源碼中生成的TEST-all.xml結(jié)果文件位于/data/data/com.example/files目錄下,要導(dǎo)出結(jié)果文件的話,需要手機擁有root權(quán)限,比較麻煩,因此下面修改了文件存放路徑,有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);
          }
          }




              
          /**
          * 判斷SD卡是否存在
          * @return
          */
          private boolean isSDCardAvaliable(){
          return Environment.getExternalStorageState()
          .equals(Environment.MEDIA_MOUNTED);
          }
          /**
          * 獲取測試結(jié)果報告文件所在的路徑
          * @param context  被測工程的context
          * @return  返回測試結(jié)果報告文件所在的路徑
          */
          private String getTestResultDir(Context context){
          String packageName = "/" + "robotium";
          String filepath = context.getCacheDir().getPath() + packageName;
          if(android.os.Build.VERSION.SDK_INT < 8){
          if(isSDCardAvaliable()){
          filepath = Environment.getExternalStorageDirectory().getAbsolutePath()+ packageName;
          }
          }else{
          if(isSDCardAvaliable()){
          filepath = Environment.getExternalStorageDirectory().getAbsolutePath()+ packageName;
          }
          }
          return filepath;
          }
          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);
          }
          }
          @Override
          public void sendStatus(int resultCode, Bundle results) {
          super.sendStatus(resultCode, results);
          switch (resultCode) {
          case REPORT_VALUE_RESULT_ERROR:
          case REPORT_VALUE_RESULT_FAILURE:
          case REPORT_VALUE_RESULT_OK:
          try {
          recordTestResult(resultCode, results);
          } catch (IOException e) {
          throw new RuntimeException(e);
          }
          break;
          case REPORT_VALUE_RESULT_START:
          recordTestStart(results);
          default:
          break;
          }
          }
          void recordTestStart(Bundle results) {
          mTestStarted = System.currentTimeMillis();
          }
          void recordTestResult(int resultCode, Bundle results) throws IOException {
          float time = (System.currentTimeMillis() - mTestStarted) / 1000.0f;
          String className = results.getString(REPORT_KEY_NAME_CLASS);
          String testMethod = results.getString(REPORT_KEY_NAME_TEST);
          String stack = results.getString(REPORT_KEY_STACK);
          int current = results.getInt(REPORT_KEY_NUM_CURRENT);
          int total = results.getInt(REPORT_KEY_NUM_TOTAL);
          mTestSuiteSerializer.startTag(null, "testcase");
          mTestSuiteSerializer.attribute(null, "classname", className);
          mTestSuiteSerializer.attribute(null, "name", testMethod);
          if (resultCode != REPORT_VALUE_RESULT_OK) {
          mTestSuiteSerializer.startTag(null, "failure");
          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);
          }
          mTestSuiteSerializer.attribute(null, "message", message);
          mTestSuiteSerializer.attribute(null, "type", reason);
          mTestSuiteSerializer.text(stack);
          }
          mTestSuiteSerializer.endTag(null, "failure");
          } else {
          mTestSuiteSerializer.attribute(null, "time", String.format("%.3f", time));
          }
          mTestSuiteSerializer.endTag(null, "testcase");
          if (current == total) {
          mTestSuiteSerializer.startTag(null, "system-out");
          mTestSuiteSerializer.endTag(null, "system-out");
          mTestSuiteSerializer.startTag(null, "system-err");
          mTestSuiteSerializer.endTag(null, "system-err");
          mTestSuiteSerializer.endTag(null, "testsuite");
          mTestSuiteSerializer.flush();
          }
          }
          @Override
          public void finish(int resultCode, Bundle results) {
          endTestSuites();
          super.finish(resultCode, results);
          }
          void endTestSuites() {
          try {
          mTestSuiteSerializer.endTag(null, "testsuites");
          mTestSuiteSerializer.endDocument();
          mTestSuiteSerializer.flush();
          mWriter.flush();
          mWriter.close();
          } catch (IOException e) {
          throw new RuntimeException(e);
          }
          }
          }



           2.修改AndroidManifest.xml文件
            將原來的:
          <instrumentation
          android:name="android.test.InstrumentationTestRunner"
          android:targetPackage="com.example" />
            修改為:
          <instrumentation
          android:name="com.example.test.instrumentation.InstrumentationTestRunner"
          android:targetPackage="com.example" />
            3.修改Run Configurations
            右鍵測試工程>Run as >Run Configurations
            在Test欄中,勾選Run all tests in the selected project,or package
            這樣每次在Eclipse中運行時才會使用新的InstrumentationTestRunner
            在Instrumentation runner處下拉選擇新寫的InstrumentationTestRunner
            點擊Apply完成設(shè)置
            4.命令行下運行測試用例
          Running all tests: adb shell am instrument -w com.android.foo/com.example.test.instrumentation.InstrumentationTestRunner
          Running a single testcase: adb shell am instrument -w -e class com.android.foo.FooTest com.android.foo/com.example.test.instrumentation.InstrumentationTestRunner
          Running multiple tests: adb shell am instrument -w -e class com.android.foo.FooTest,com.android.foo.TooTest com.android.foo/com.example.test.instrumentation.InstrumentationTestRunner
            命令行下運行測試用例與平時一樣,只要將原來的InstrumentationTestRunner換成新的InstrumentationTestRunner就行,
            需要注意的是,由于每次命令行執(zhí)行完畢,都會覆蓋原有的TEST-all.xml文件,即如果采用Running a single testcase方式運行多個測試用例集,則最后結(jié)果只會記錄最后一個用例集,因此建議采用Running multiple tests方式
            5.運行完成后將手機中的文件導(dǎo)出
            adb -s $ANDROID_AVD_DEVICE pull /mnt/sdcard/rototium/TEST-all.xml
            其中$ANDROID_AVD_DEVICE為參數(shù)化的手機序列號
            6.在jenkins中任務(wù)構(gòu)建完成后即可使用Publish JUnit test result report插件分析得出單元測試報告了

          posted on 2013-11-14 10:48 順其自然EVO 閱讀(519) 評論(0)  編輯  收藏 所屬分類: android

          <2013年11月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 遵化市| 龙胜| 马山县| 万年县| 邵东县| 明水县| 松原市| 忻城县| 南投县| 阿尔山市| 夏河县| 城市| 凤台县| 武夷山市| 和政县| 大宁县| 郸城县| 浦东新区| 开原市| 称多县| 若尔盖县| 垣曲县| 玉林市| 龙陵县| 松溪县| 沁源县| 镇康县| 尉氏县| 常州市| 禄丰县| 龙游县| 边坝县| 徐汇区| 德江县| 平陆县| 南安市| 渑池县| 亚东县| 台北市| 淅川县| 垦利县|