标签:
随着我们测试脚本中的用例越来越多,我们不可能将所有的用例都放在同一个脚本中,所以我们需要优化我们的结构。将脚本放在一个文件夹中,再通过别的脚本来执行脚本。这样,我们也可以有选择性的执行我们的脚本
先来看一下现在的目录结构
测试脚本统一放到了test_case文件夹中,注意这个文件夹中要添加一个__init__.py的文件,只要是这个名字就行了,内容为空
因为我们要导入这个文件夹中的module,所以我们先构建一个package,而package必须包含一个__init__.py文件
然后来看下我们的脚本
这是again.py
#coding:utf-8 import unittest from selenium import webdriver from time import sleep import HTMLTestRunner import time class againTest(unittest.TestCase): @classmethod def setUpClass(cls): print(‘开始执行againTest‘) desired_caps = {} desired_caps[‘platformName‘] = ‘iOS‘ desired_caps[‘deviceName‘] = ‘iPhone 6‘ cls.driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘,desired_caps) @classmethod def tearDownClass(cls): cls.driver.quit() print(‘执行结束‘) def test_sure_again(self): u‘‘‘点击sure后点击again,回合数加1‘‘‘ self.driver.find_element_by_id(‘sureButton‘).click() exround = int(self.driver.find_element_by_id(‘roundLab‘).text) sleep(2) self.driver.find_element_by_id(‘Again‘).click() self.assertEqual(int(self.driver.find_element_by_id(‘roundLab‘).text),exround+1,‘回合数不对‘) if __name__ == ‘__main__‘: suite = unittest.TestSuite() suite.addTest(againTest(‘test_sure_again‘)) timestr = time.strftime(‘%Y-%m-%d %X‘,time.localtime(time.time())) filename = ‘/Users/lihui/Documents/PycharmProjects/AppDemo/report/‘+timestr+‘.html‘ fp = open(filename,‘wb‘) runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title=‘result‘, description=‘report‘ ) runner.run(suite) fp.close()
reset.py
#coding:utf-8 import unittest from selenium import webdriver from time import sleep import HTMLTestRunner import time class resetTest(unittest.TestCase): @classmethod def setUpClass(cls): print(‘开始执行resetTest‘) desired_caps = {} desired_caps[‘platformName‘] = ‘iOS‘ desired_caps[‘deviceName‘] = ‘iPhone 6‘ cls.driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘,desired_caps) @classmethod def tearDownClass(cls): cls.driver.quit() print(‘执行结束‘) def test_reset(self): u‘‘‘直接点击reset,回合数变为1‘‘‘ self.driver.find_element_by_id(‘sureButton‘).click() sleep(2) self.driver.find_element_by_id(‘Again‘).click() self.driver.find_element_by_id(‘resetButton‘).click() self.assertEqual(int(self.driver.find_element_by_id(‘roundLab‘).text),1,‘回合数不对‘) if __name__ == ‘__main__‘: suite = unittest.TestSuite() suite.addTest(resetTest(‘test_reset‘)) timestr = time.strftime(‘%Y-%m-%d %X‘,time.localtime(time.time())) filename = ‘/Users/lihui/Documents/PycharmProjects/AppDemo/report/‘+timestr+‘.html‘ fp = open(filename,‘wb‘) runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title=‘result‘, description=‘report‘ ) runner.run(suite) fp.close()
all_tests.py
#coding=utf-8 import unittest import sys sys.path.append("/test_case") #添加test_case目录 from test_case import again,reset import HTMLTestRunner import time alltestnames = [again.againTest, reset.resetTest, ] #添加执行的测试用例 testunit = unittest.TestSuite() for test in alltestnames: testunit.addTest(unittest.makeSuite(test)) timestr = time.strftime(‘%Y-%m-%d %X‘,time.localtime(time.time())) filename = ‘/Users/lihui/Documents/PycharmProjects/AppDemo/report/‘+timestr+‘.html‘ fp = open(filename,‘wb‘) runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title=‘result‘, description=‘report‘ ) runner.run(testunit) fp.close() print(‘执行完毕,报告路径:‘+filename)
执行all_tests之后生成的测试报告
比起Appium学习实践(三)测试用例脚本以及测试报告输出中的测试报告,可以看到增加了中文描述,这样报告看起来也更加明了些
测试报告中根据2个测试脚本文件分开显示测试结果,实际中,我们可以将一个模块的测试用例写到一个脚本中,这样更方便管理
如果我们要执行单个模块的测试用例只要执行test_case中对应的脚本就行了
当然,如果要执行多个但不是全部的脚本,则在all_tests.py中alltestnames进行设置,要执行哪些就添加哪些
Ps:感觉到目前为止的都是基本的东西。后面会添加一些自己在实际编写脚本中遇到的坑。。
标签:
原文地址:http://www.cnblogs.com/delete31/p/5287551.html