- 项目文件结构
common:
存放经常被引用的文件,如HTMLTestRunner
Locator:
存放页面元素的位置和对应url,如oper_userName = (By.ID, “username”)
pages:
对于网站有不同的页面,可分开写
reports:
存放测试生成的报告
screenshots:
存放测试中截屏的图片
每个用例都是一个class。拥有自己的浏览器启动函数。具体形式如下:
import sys
import time
import unittest
from selenium import webdriver
class TestCase1(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(getChromeDriver())
####中间插入该用例的其他函数
def tearDown(self):
self.driver.quit()
如果有很多个用例,则需要一个总函数来进行批量运行,并生成测试报告。单个运行太麻烦了。首先建立一个casesuit,在当前文件中进行查找对应的case。
import os,sys
import unittest
import time
import HtmlTestRunner
os_path = os.getcwd()
casesPath = os_path + "\\test_case"
reportPath = os_path + "\\Reports\\"
#templatePath = os_path + "\\template\\report_template.html"
'''如果HtmlTestRunner 报错请执行pip install html-testRunner '''
def CreatSuite():
suite=unittest.TestSuite()
discover=unittest.defaultTestLoader.discover(casesPath, pattern='test_*.py', top_level_dir=None)
for test_case in discover:
suite.addTests(test_case)
return suite
if __name__ == "__main__":
all_test_cases = CreatSuite()
currentTime = str(int(time.time()))
if not os.path.exists(reportPath):
os.makedirs(reportPath)
reportName = 'EPT_TestReport_' + currentTime + '.html'
reportName = reportPath + reportName
testRunner = HtmlTestRunner.HTMLTestRunner(output=reportPath)
testRunner.run(all_test_cases)