Android-Junit-Report测试报告生成——Android自动化测试学习历程
视频地址:
http://www.chuanke.com/v1983382-135467-384869.html
这个内容其实已经在用了,我在上一篇文章robotium—只有apk文件的测试中已经讲过这个内容了,并且自己也用Python+wxpython写了界面程序,来实现跑case+获取xml运行结果+xml转html+发送邮件的功能
主要内容:
一、测试需求
1、统计每个case的执行时间
2、哪些case成功、失败
3、失败的case给出log
4、产生网页html报告结果
二、环境部署
以robotium为例,需要的环境:
1、JDK1.7、Android开发环境
2、Robotium的jar包
3、android-junit-report.jar包
三、报告生成原理
去官网瞅瞅:http://zutubi.com/,通过点击open source可进入下载页面下载
quick start
For the impatient, here is an overview of how to integrate the runner with Ant builds. Note all modifications are made to your test project, i.e. the project which implements the JUnit tests:
- Grab the jar from the downloads page and add it to your
libs/
directory. - Edit
AndroidManifest.xml
to setandroid:name
in the<instrumentation>
tag to:com.zutubi.android.junitreport.JUnitReportTestRunner
. - Edit
ant.properties
to add the line:test.runner=com.zutubi.android.junitreport.JUnitReportTestRunner
- Run your tests as you would normally:
$ ant debug install test
- Pull the resulting XML report from the device (from the application under test's internal storage directory):
$ adb pull /data/data/<main app package>/files/junit-report.xml
- Integrate the XML with your chosen build tool.
接下来就是原理:
1、com.zutubi.android.junitreport.JUnitReportTestRunner——具体见上面描述,需要修改的地方是两个,一个是instrumentation的tag,一个是Run As的Run Configuration,修改Instrumentation runner的值
2、调用机制:
三层封装:
Junit Report---》Robotium---》Instrumentation
四、脚本实现自动化后续收集工作
脚本1——运行testcase
脚本2——把xml从手机内存卡pull出来
脚本3——把xml转换成html
脚本4——把html的报告合并到手工、压力、性能报告中去
脚本5——发送邮件周知即可
Done!还是需要自己去写。。。
代码见下:
# -*-coding:gb2312-*- #Function:界面程序,要去调用已经写好的重签名、install重签名后的apk和testapk,以及
#Author:LiXia
#Date:2015.01.16 #功能:要写界面程序 import wx
import os
import smtplib
import time
from xml.dom import minidom
import sys
from pyh import *
import webbrowser
import subprocess reload(sys)
sys.setdefaultencoding('gb2312') #获取时间戳
def timestamp(style = '%Y-%m-%d %H:%M:%S'):
return time.strftime(style, time.localtime()) ##mail_server请修改成自己公司的对应的正确的邮箱服务器,port也根据需要填写好
def sendMail(data, mail_from, mail_to, mail_title, mail_server=r'mail.x.y.net', mail_port=25):
try:
handle = smtplib.SMTP(mail_server, mail_port)
#handle = smtplib.SMTP('mail.360.cn', 25)
handle.ehlo()
handle.starttls()
handle.ehlo() mail_data = timestamp() mail_data = ""; msg = "From: %s\r\nTo: %s\r\nData: %s\r\nContent-Type: text/html;charset=gb2312\r\nSubject: %s \r\n\r\n %s\r\n" % (mail_from, str(mail_to), mail_data, mail_title, data)
handle.sendmail('%s' % mail_from, mail_to, msg)
handle.close()
return True
except Exception, e:
print e
logobj.error('发信失败,程序退出')
return False def XMLtoHTML():
# 读取xml文档内容
doc = minidom.parse('junit-report.xml') #跑成功的case,存一下
RunSuccess = []
#跑失败的case,存一下
RunFailure = [] _document = doc.documentElement
#testsuites与testcase之间不是一个父子的关系
testsuites = _document.getElementsByTagName('testsuite')
for testsuite in testsuites:
pass
# print testsuite
# print testsuite.nodeName
# print testsuite.nodeValue
# print testsuite.attributes['name'].value testcases = _document.getElementsByTagName('testcase')
i = 0
j = 0
for testcase in testcases:
print testcase.nodeName
print testcase.attributes['classname'].value
print testcase.attributes['name'].value
print testcase.attributes['time'].value classname = testcase.attributes['classname'].value.encode('gb2312')
name = testcase.attributes['name'].value.encode('gb2312')
time = testcase.attributes['time'].value.encode('gb2312') if testcase.firstChild != None:
print 'Run Failure'
failure = testcase.firstChild
print failure.nodeName
print failure.attributes['message'].value.encode('gb2312')
print failure.attributes['type'].value.encode('gb2312')
print failure.firstChild.nodeValue failureMessage = failure.attributes['message'].value.encode('gb2312')
failureType = failure.attributes['type'].value.encode('gb2312')
failureContent = failure.firstChild.nodeValue.encode('gb2312') # RunFailure.append({'testcase.classname': classname, 'testcase.name': name, 'testcase.time': time,
# 'failureMessage': failureMessage, 'failureType': failureType, 'failureContent': failureContent})
RunFailure.append([classname, name, time, failureMessage, failureType, failureContent]) else:
#说明没有failure的这一项,那就是运行成功了
print 'Run Success!!'
# RunSuccess.append({'testcase.classname': classname, 'testcase.name': name, 'testcase.time': time})
RunSuccess.append([classname, name, time]) print '以下是成功的case的基本信息'
for i in range(0, len(RunSuccess)):
print RunSuccess[i] #print '以下是失败的case的基本信息' #for j in range(0, len(RunFailure)):
# print RunFailure[j] #直接生成所需要的html文档
page = PyH('路由器卫士自动化测试Case运行结果') #显示一个汇总的表格,总共多少Case,成功多少,失败多少
page <<h2('Table of Run Cases')
mytab = page << table(border = "")
tr1 = mytab << tr()
tr1 << td('TestCase-AllNum')
tr1 << td('TestCase-SuccessNum')
tr1 << td('TestCase-FailureNum') numAll = len(RunSuccess) + len(RunFailure)
numSuccess = len(RunSuccess)
numFailure = len(RunFailure) tr2 = mytab << tr()
tr2 << td(numAll)
tr2 << td(numSuccess)
tr2 << td(numFailure) #TestCase 成功的
page << h2('Tabel of Run Success')
mytab = page << table(border = "")
tr1 = mytab << tr()
tr1 << td('TestCase-ClassName')
tr1 << td('TestCase-Name')
tr1 << td('TestCase-Time(S)') for i in range(len(RunSuccess)):
tr2 = mytab << tr()
for j in range(len(RunSuccess[i])):
tr2 << td(RunSuccess[i][j]) #TestCase 失败的
page << h2('Tabel of Run Failure')
mytab = page << table(border = "")
tr1 = mytab << tr()
tr1 << td('TestCase-ClassName')
tr1 << td('TestCase-Name')
tr1 << td('TestCase-Time(S)')
tr1 << td('TestCase-FailureMessage')
tr1 << td('TestCase-FailureType')
tr1 << td('TestCase-FailureContent') for i in range(len(RunFailure)):
tr2 = mytab << tr()
for j in range(len(RunFailure[i])):
tr2 << td(RunFailure[i][j]) page.printOut("caseResult.html")
# webbrowser.open("caseResult.html") #重签名的函数,是在cmd里面调的
def reSign(event):
cmd1 = r'java -jar re-sign.jar'
# os.system(cmd1)
subprocess.Popen(cmd1) def getPath2(event):
dirname = 'C:'
dialog = wx.FileDialog(None, "choose a file",dirname,"","*.*",wx.OPEN)
if dialog.ShowModal() == wx.ID_OK:
fileChoose2.SetValue(dialog.GetPath()) dialog.Destroy() def getPath3(event):
dirname = 'C:'
dialog = wx.FileDialog(None, "choose a file",dirname,"","*.*",wx.OPEN)
if dialog.ShowModal() == wx.ID_OK:
fileChoose3.SetValue(dialog.GetPath()) dialog.Destroy() #安装apk程序的函数,第二步和第三步一起来啦
def installApk2(event):
cmd1 = r'adb install' + ' ' + fileChoose2.GetValue()
# ret = os.popen(cmd1)
ret = subprocess.Popen(cmd1, stdout = subprocess.PIPE)
result = ret.stdout.read()
print '返回值:' + result
if ('Failure' in result):
print '失败啦!'
dlg = wx.MessageDialog(None, result, '安装失败', wx.YES_NO)
if dlg.ShowModal() == wx.ID_YES:
dlg.Close(True)
dlg.Destroy()
elif ('Success' in result):
print '成功啦!'
dlg = wx.MessageDialog(None, result, '安装成功', wx.YES_NO)
if dlg.ShowModal() == wx.ID_YES:
dlg.Close(True)
dlg.Destroy() def installApk3(event):
cmd2 = r'adb install' + ' ' + fileChoose3.GetValue()
# ret = os.popen(cmd2)
ret = subprocess.Popen(cmd2, stdout = subprocess.PIPE)
result = ret.stdout.read()
print '返回值:' + result
if ('Failure' in result):
print '失败啦!'
dlg = wx.MessageDialog(None, result, '安装失败', wx.YES_NO)
if dlg.ShowModal() == wx.ID_YES:
dlg.Close(True)
dlg.Destroy()
elif ('Success' in result):
print '成功啦!'
dlg = wx.MessageDialog(None, result, '安装成功', wx.YES_NO)
if dlg.ShowModal() == wx.ID_YES:
dlg.Close(True)
dlg.Destroy() #通过appt获取包名,看看行不行
def getPackageInfo(apkPath):
cmd1 = r'aapt.exe' + ' ' + r'dump badging' + ' ' + apkPath
ret = subprocess.Popen(cmd1, stdout = subprocess.PIPE)
result = ret.stdout.read()
print '返回值:' + result print '开始找index位置:\n'
indexPackageName1 = result.find("name")
indexPackageName2 = result.find("version")
indexMainActivity1 = result.find("launchable-activity")
indexMainActivity2 = result.find("label", result.find("launchable-activity")) print indexPackageName1,indexPackageName2,indexMainActivity1,indexMainActivity2 #然后根据这几项提取我需要的内容即可 #这里面应该用len(name='),但是因为有特殊字符‘,导致出现了问题,以后再想,反正这个是搞定了 packageName = ''
for i in range(indexPackageName1+6, indexPackageName2-2):
packageName += result[i] print packageName mainActivity = ''
for i in range(indexMainActivity1+27, indexMainActivity2-3):
mainActivity += result[i] print mainActivity return packageName #跑case啊,还是在cmd里面来的
def runCase(event):
#先用这个来吧,之后再说获取包名和走单个case的情况
# cmd1 = r'adb shell am instrument -w com.qihoo.router.test/com.zutubi.android.junitreport.JUnitReportTestRunner'
#传进来一个参数,这个参数是获取到的packageName,这样就不用写死了
packageName = getPackageInfo(fileChoose3.GetValue())
print packageName
cmd1 = r'adb shell am instrument -w' + r' ' + packageName + '/' + 'com.zutubi.android.junitreport.JUnitReportTestRunner'
print cmd1
# os.system(cmd1)
subprocess.Popen(cmd1) #pull结果啊,从手机端pull到PC端
def pullResult(event):
#后面不加路径的话,应该会直接pull到当前目录吧,试试
packageName = getPackageInfo(fileChoose2.GetValue())
cmd1 = r'adb pull /data/data/' + packageName + '/files/junit-report.xml'
print cmd1
# os.system(cmd1)
ret = subprocess.Popen(cmd1, stdout = subprocess.PIPE)
result = ret.stdout.read()
dlg = wx.MessageDialog(None, result, 'xml测试结果从手机pull到PC端', wx.YES_NO)
if dlg.ShowModal() == wx.ID_YES:
dlg.Close(True)
dlg.Destroy() #转换xml为html,并发送邮件
def sendHtmlResult(event):
XMLtoHTML()
f = open("caseResult.html")
contents = f.read() #这里的mail_to应该是一个列表,最开始的时候,直接写的就是字符串,它无法解析成功
#下面是错误写法:
#mail_to_error = "lixia-xy@360.cn, sunjingjing@360.cn"
#mail_to = ["lixia-xy@360.cn", "sunjingjing@360.cn", "jialiwei-s@360.cn", "mengmo@360.cn", "cuifang@360.cn"]
mail_to = ["xxx@yyy.com"] sendMail(contents, "xxx@yyy.com", mail_to, "路由器卫士自动化回归运行结果"); app = wx.App()
frame = wx.Frame(parent = None, id = -1, title = '安卓自动化测试', size = (870, 600))
bkg = wx.Panel(frame) #往里面添加所需要的控件
#第一步,重签名,选择需要重签名的apk(目前只能对非加固的包做处理)
#第二步,安装程序,重签名后的apk和自己生成的testapk,通过cmd
#第三步,开始运行测试,可以给出选项吧,case全跑,或者给出一个下拉列表,想跑哪些自动勾选,做成一个可多选的那个控件??
#第四步,收集测试结果,其实就是用cmd命令通过adb pull把生成的xml文件搞出来
#第五步,转换xml为html,并发送邮件??这个程序已经写好,到时候直接调就行 Info = wx.StaticText(bkg, -1, '请大家按照下面的步骤来,就能完成测试、结果收集以及邮件通知啦!!', pos = (40, 30)) stepOne = wx.StaticText(bkg, -1, '第一步:重签名', pos = (40, 80))
#fileChoose = wx.TextCtrl(bkg, -1, '选择你需要重签名的apk文件', size = (380, -1), pos = (200, 80))
#OKButton1 = wx.Button(bkg, -1, '重签名', pos = (600, 80))
OKButton1 = wx.Button(bkg, -1, '重签名', pos = (200, 75))
OKButton1.Bind(wx.EVT_BUTTON, reSign) stepTwo = wx.StaticText(bkg, -1, '第二步:安装待测程序', pos = (40, 150))
fileChoose2 = wx.TextCtrl(bkg, -1, '选择你需要测试的重签名后的apk文件', size = (380, -1), pos = (200, 150))
pathButton2 = wx.Button(bkg, -1, '选择文件', pos = (600, 145))
pathButton2.Bind(wx.EVT_BUTTON, getPath2)
OKButton2 = wx.Button(bkg, -1, '安装', pos = (700, 145))
OKButton2.Bind(wx.EVT_BUTTON, installApk2) stepThree = wx.StaticText(bkg, -1, '第三步:安装Test程序', pos = (40, 220))
fileChoose3 = wx.TextCtrl(bkg, -1, '选择你生成的testapk文件', size = (380, -1), pos = (200, 220))
pathButton3 = wx.Button(bkg, -1, '选择文件', pos = (600, 215))
pathButton3.Bind(wx.EVT_BUTTON, getPath3)
OKButton3 = wx.Button(bkg, -1, '安装', pos = (700, 215))
OKButton3.Bind(wx.EVT_BUTTON, installApk3) stepFour = wx.StaticText(bkg, -1, '第四步:开测', pos = (40, 290))
OKButton4 = wx.Button(bkg, -1, '全Case回归', pos = (200, 285))
OKButton4.Bind(wx.EVT_BUTTON, runCase) stepFive = wx.StaticText(bkg, -1, '第五步:收集测试结果', pos = (40, 360))
OKButton5 = wx.Button(bkg, -1, '获取测试xml结果', pos = (200, 355))
OKButton5.Bind(wx.EVT_BUTTON, pullResult) stepSix = wx.StaticText(bkg, -1, '第六步:回归结果周知', pos = (40, 430))
OKButton6 = wx.Button(bkg, -1, '发送邮件告诉大家啦!!', pos = (200, 425))
OKButton6.Bind(wx.EVT_BUTTON, sendHtmlResult) frame.Show()
app.MainLoop()
Android-Junit-Report测试报告生成——Android自动化测试学习历程的更多相关文章
- 百度Cafe原理--Android自动化测试学习历程
主要讲解内容及笔记: 一.Cafe原理 Cafe是一款自动化测试框架,解决问题:跨进程测试.快速深度测试 官网:http://baiduqa.github.io/Cafe/ Cafe provides ...
- 截图原理(一)——Android自动化测试学习历程
把两节的内容汇总起来,第一节讲的是如何在apk中直接进行截屏,用到了Robotium的Solo类的takeScreenShot方法,有一个小的demo,以及从方法一直往里钻,知道它具体是怎么进行截屏的 ...
- Instrumentation类——Android自动化测试学习历程
这里需要把Instrumentation类的视频的上.中.下三集一起看,把内容总结一下... 视频地址: http://study.163.com/course/courseLearn.htm?cou ...
- Monkey原理初步和改良优化--Android自动化测试学习历程
章节:自动化基础篇——Monkey原理初步和改良优化(第三讲) 主要讲解内容与笔记: 一.理论知识: 直接看文档,来了解monkey的概念.基本原理,以及如何使用. First,what is And ...
- 跨进程(同一app不同进程之间通信)——Android自动化测试学习历程
视频地址:http://study.163.com/course/courseLearn.htm?courseId=712011#/learn/video?lessonId=877122&co ...
- 截图原理(二)——android自动化测试学习历程
接上一篇(截图原理) 视频地址:http://study.163.com/course/courseLearn.htm?courseId=712011#/learn/video?lessonId=87 ...
- Selenium原理初步--Android自动化测试学习历程
章节:自动化基础篇——Selenium原理初步(第五讲) 注:其实所有的东西都是应该先去用,但是工具基本都一样,底层都是用的最基础的内容实现的,测试应该做的是: (1)熟练使用工具,了解各个工具的利弊 ...
- 自动化预备知识上&&下--Android自动化测试学习历程
章节:自动化基础篇——自动化预备知识上&&下 主要讲解内容及笔记: 一.需要具备的能力: 测试一年,编程一年,熟悉并掌握业界自动化测试工具(monkey--压力测试.monkeyrun ...
- Appium原理初步--Android自动化测试学习历程
章节:自动化基础篇——Appium原理初步(第七讲) 本期关键词: Appium.跨语言跨平台.Bootstrap 主要讲解内容及笔记: 一.what is appium 一种封装了uiautomat ...
随机推荐
- VS2013添加使用lib的方法
使用第三方厂家的库,一般都会有三个文件: xxx.h xxx.lib xxx.dll 头文件很明显,就在需要用到的文件里面include就好. lib文件,可以直接在IDE中添加,具体步骤有两个: 1 ...
- C语言中的 extern 关键字
今天在 BLE 中看到很多 extern 关键字,现在总结一下: extern 关键字主要用于在一个c文件中要用到另一个c文件中的变量或者函数. example: #extern_base.c ; # ...
- 查杀 oracle sql 卡死的进程
--查出SID名字select * from v$access where object ='PKG_SMSSEND_PROC';--通过sid查出spidselect spid,osuser, s ...
- [转帖]DAS、NAS、SAN、iSCSI 存储方案概述
目前服务器所使用的专业存储方案有DAS.NAS.SAN.iSCSI几种.存储根据服务器类型可以分为:封闭系统的存储和开放系统的存储: (1)封闭系统主要指大型机. (2)开 ...
- Activity和Service是否是在同一个进程中运行。
一般情况下,Activity和Service在同一个包名内,并且没有设定属性android:process=":remote",两者在同一个进程中. 因为一个进程只有一个UI线程, ...
- 【uTenux实验】信号量
信号量(semaphore)是一个用来指示可用的资源并将可用资源的数量以数值的形式表示出来的对象.当使用一组资源时,信号量用来实现互斥控制和同步.uTenux提供了信号量出来的API,可以很方便地使用 ...
- Hibernate5.2关联关系之单向一对多(一)
Hibernate5.2之单向一对多 一. 简介 Hibernate中最复杂的应该就是各种关联(单向一对多.单向多对一.双向一对多.一对一.多对多)关系的映射,于是笔者就想着去写一些关于Hibe ...
- java io 输入输出流
数据流分类: 流序列中的数据既可以是未经加工的原始二进制数据, 也可以是经一定编码处理后符合某种格式规定的特定数据. 因此Java中的流分为两种: 1) 字节流:数据流中最小的数据单元是字节 2) 字 ...
- (LinkedList)2. Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 17. Word Break && Word Break II
Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...