之前Appium学习实践(二)Python简单脚本以及元素的属性设置中的脚本,会有一个问题,就是在每个测试用例完成之后都会执行tearDown,然后重新setUp,这样导致脚本的执行效率偏低,而且会有大量的重复性的步骤。当然有时测试脚本确实需要每次都重新开启一个干净的app。
下面说下怎样让一个测试用例执行完毕后接着执行后面的测试用例而不是重置。
#coding:utf-8
import unittest
import os
from selenium import webdriver
from time import sleep
import HTMLTestRunner
import time class Dttest(unittest.TestCase):
@classmethod
def setUpClass(cls):
print('start setup')
desired_caps = {}
desired_caps['platformName'] = 'iOS'
desired_caps['deviceName'] = 'iPhone 5s' cls.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps) @classmethod
def tearDownClass(cls):
cls.driver.quit()
print('tearDown') def test_test(self):
sleep(10)
print('test passed') def test_click(self):
self.driver.find_element_by_name('point:').click()
sleep(5)
print('click passed') if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(Dttest('test_test'))
suite.addTest(Dttest('test_click'))
unittest.TextTestRunner(verbosity=2).run(suite)
这是使用的是Python的unittest中的setUpClass和tearDownClass,请见官方文档:https://docs.python.org/2/library/unittest.html
会先执行test_test,执行完毕后再执行test_click,两个测试用例执行完毕后再执行tearDownClass
----------------------------------------这里是分割线----------------------------------------
如果我们只能在命令行看到运行结果的话,有点不美,如果最后能有个report是不是能更好呢
下面来说下测试报告的输出,用的是HTMLTestRunner
代码如下
#coding:utf-8
import unittest
import os
from selenium import webdriver
from time import sleep
import HTMLTestRunner
import time class Dttest(unittest.TestCase):
@classmethod
def setUpClass(cls):
print('start setup')
desired_caps = {}
desired_caps['platformName'] = 'iOS'
desired_caps['deviceName'] = 'iPhone 5s' cls.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps) @classmethod
def tearDownClass(cls):
cls.driver.quit()
print('tearDown') def test_slideAndPressSure(self):
sleep(10)
print('test passed') def test_click(self):
self.driver.find_element_by_name('point:').click()
sleep(5)
print('click passed') if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(Dttest('test_slideAndPressSure'))
suite.addTest(Dttest('test_click'))
#unittest.TextTestRunner(verbosity=2).run(suite)
timestr = time.strftime('%Y-%m-%d %X',time.localtime(time.time()))
filename = '/Users/lihui/Documents/PycharmProjects/test/report/'+timestr+'.html'
fp = open(filename,'wb')
runner = HTMLTestRunner.HTMLTestRunner(
stream=fp,
title='result',
description='report'
)
runner.run(suite)
fp.close()
最后会在我们设定的文件夹中生成一个以日期命名的html的报告,当然也可以自行改名(文件夹的名字先自己建好)
结果如下
感觉还是有点简陋。、后面再研究下
Ps:生成的报告中中文乱码,将HTMLTestRunner.py中的latin-1改成utf-8
最后附上Python unittest的中文文档地址http://pyunit.sourceforge.net/pyunit_cn.html#USING