Appium跑python脚本自动生成简单测试结果报告
1.首先在python目录添加HTMLTestRunner.py文件
HTMLTestRunner.py下载地址:
http://tungwaiyip.info/software/HTMLTestRunner.html
(1)找到python的site-packages目录,我的目录为/Library/Python/2.7/site-packages;(2)将HTMLTestRunner.py文件拷贝到以上的文件夹内。
2.Python脚本内添加相应代码
(1)在脚本最前面添加import
HTMLTestRunner代码;(2)在脚本最后面添加生成报告样式的代码;这里写一个简单的脚本,代码如下:
import unittest
import os
from selenium import webdriver
from time import sleep
import HTMLTestRunner
import time
class Dttest(unittest.TestCase):
@classmethod
defsetUpClass(cls):
print('start
setup')
desired_caps
= {}
desired_caps['platformName']=
'iOS'
desired_caps['deviceName']
= 'iPhone 6'
desired_caps['autoAcceptAlerts']
= 'true'
cls.driver=
webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
@classmethod
deftearDownClass(cls):
cls.driver.quit()
print('tearDown')
deftest_sleep(self):#用例
sleep(10)
print('sleep
passed')
deftest_clicktap1(self):#用例
self.driver.find_element_by_name('爱物').click()
sleep(5)
print('clickpassed')
deftest_clicktap2(self):#用例
self.driver.find_element_by_name('推荐').click()
sleep(5)
print('click
passed')
if __name__ == '__main__':
suite
=unittest.TestSuite()
suite.addTest(Dttest('test_sleep'))#需要测试的用例就addTest,不加的就不会运行
suite.addTest(Dttest('test_clicktap1'))
suite.addTest(Dttest('test_clicktap2'))
#unittest.TextTestRunner(verbosity=1).run(suite)
timestr
=time.strftime('%Y-%m-%d
%X',time.localtime(time.time()))#本地日期时间作为测试报告的名字
filename
='/Users/zhuquan/Documents/Test-ZQ/report/'+timestr+'.html'#这个路径改成自己的目录路径
fp
=open(filename,'wb')
runner
=HTMLTestRunner.HTMLTestRunner(
stream=fp,
title='result',
description='report'
)
runner.run(suite)
fp.close()
这样运行完脚本就可自动生成简单测试结果报告了,如下图所示: