1.自动化测试框架
1.1unittest和junit的区别
- 共同点:单元测试框架
- unittest:UI界面功能的单元测试框架
- Junit:基于代码的单元测试框架(白盒)
1.2unittest框架解析
1.2.1测试固件:
- setUp():执行测试用例前的准备工作
- tearDown():执行测试用例后的清理工作
1.2.2测试套件:
1.2.2.1测试用例的运行顺序
-
0-9 > A-Z > a-z -
忽略测试用例的执行
import time
from selenium import webdriver
import unittest
class Test11(unittest.TestCase):
def setUp(self):
print("-----setup-----")
self.driver = webdriver.Chrome()
self.url = "https://www.baidu.com/"
self.driver.maximize_window()
def tearDown(self):
print("----down-----")
self.driver.quit()
def test_1baidu(self):
driver = self.driver
url = self.url
driver.get(url)
driver.find_element_by_id("kw").send_keys("hhh")
driver.find_element_by_id("su").click()
time.sleep(3)
def test_Abaidu(self):
driver = self.driver
url = self.url
driver.get(url)
driver.find_element_by_id("kw").send_keys("111")
driver.find_element_by_id("su").click()
time.sleep(3)
def print(self):
print("1111111111111111")
def test_asiduxinwen(self):
driver = self.driver
url = self.url
driver.get(url)
driver.find_element_by_link_text("新闻").click()
time.sleep(3)
if __name__ == "__main__":
unittest.main()
1.2.2.2addtest:每次把一个测试脚本中的一个测试用例加载进测试套件
1.2.2.3makesuit:把一个测试脚本中的所有测试用例加载进测试套件
1.2.2.4TestLoader:把一个测试脚本中的所有测试用例加载进测试套件
suite1 = unittest.TestLoader().loadTestsFromTestCase(testbaidu1.Baidu1)
suite2 = unittest.TestLoader().loadTestsFromTestCase(testbaidu2.Baidu2)
suite = unittest.TestSuite([suite1, suite2])
return suite
1.2.2.5discover:把一个文件夹,所有以某种形式命名的脚本中的所有测试用例都加载到测试套件中。
discover = unittest.defaultTestLoader.discover("../src0811", pattern="testbaidu*.py", top_level_dir=None)
return discover
1.2.2.6verbosity
if __name__ == "__main__":
unittest.main(verbosity=2)
1.2.2.7 断言
- 手工测试用例:测试环境,测试步骤,测试数据,预期结果
def test_Abaidu(self):
driver = self.driver
url = self.url
driver.get(url)
driver.find_element_by_id("kw").send_keys("杨洋")
driver.find_element_by_id("su").click()
time.sleep(6)
title=driver.title
self.assertEqual("杨洋_百度搜索",title,msg="判断错误")
time.sleep(3)
1.2.2.8HTML报告
- 总结分析测试用例执行的过程那些成功,那些事败,并分析失败原因,记录结果。
import HTMLTestRunner
import os
import sys
import time
import unittest
def createsuite():
discovers = unittest.defaultTestLoader.discover("../src0811", pattern="testbaidu*.py", top_level_dir=None)
print(discovers)
return discovers
if __name__ == "__main__":
curpath = sys.path[0]
print(sys.path)
print(sys.path[0])
if not os.path.exists(curpath + '/resultreport'):
os.makedirs(curpath + '/resultreport')
now = time.strftime("%Y-%m-%d-%H %M %S", time.localtime(time.time()))
print(time.time())
print(time.localtime(time.time()))
filename = curpath + '/resultreport/' + now + 'resultreport.html'
with open(filename, 'wb') as fp:
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"测试报告",
description=u"用例执行情况", verbosity=2)
suite = createsuite()
runner.run(suite)
1.2.2.9错误截图
from selenium import webdriver
import unittest
import time
import os
class Baidu1(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url = "http://www.baidu.com/"
self.verificationErrors=[]
self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([],self.verificationErrors)
@unittest.skip("skipping")
def test_baidusearch(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys(u"大虞海棠")
driver.find_element_by_id("su").click()
time.sleep(6)
def test_baidu(self):
driver = self.driver
driver.get(self.base_url)
driver.maximize_window()
driver.find_element_by_id("kw").send_keys("向往的生活")
driver.find_element_by_id("su").click()
time.sleep(6)
print(driver.title)
try:
self.assertEqual(driver.title, u"百度一下,你就知道", msg="不相等")
except:
self.saveScreenShot(driver, "hao.png")
time.sleep(5)
def saveScreenShot(self, driver, file_name):
if not os.path.exists("./image"):
os.makedirs("./image")
now = time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
driver.get_screenshot_as_file("./image/"+now+"-"+file_name)
time.sleep(3)
if __name__ == "__main__":
unittest.main()
1.2.2.10数据驱动
from selenium import webdriver
import unittest
import time
from ddt import ddt, unpack, data, file_data
import sys, csv
def getTxt(file_name):
rows = []
path = sys.path[0]
with open(path + '/data/' + file_name, 'rt', encoding="utf-8") as f:
readers = csv.reader(f, delimiter=',', quotechar='|')
next(readers, None)
for row in readers:
temprows = []
for i in row:
temprows.append(i)
rows.append(temprows)
return rows
@ddt
class Baidu9(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url = "http://www.baidu.com/"
self.driver.maximize_window()
self.verificationErrors = []
self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
@file_data('test_baidu_data.json')
def test_baidu1(self, value):
driver = self.driver
driver.get(self.base_url + "/")
driver.maximize_window()
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys(value)
driver.find_element_by_id("su").click()
time.sleep(6)
@unittest.skip("skipping")
@data(*getTxt('test_baidu_data.txt'))
@unpack
def test_baidu2(self, value, expected_value):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys(value)
driver.find_element_by_id("su").click()
driver.maximize_window()
time.sleep(6)
self.assertEqual(expected_value, driver.title, msg="和预期搜索结果不一致!")
print(expected_value)
print(driver.title)
time.sleep(6)
if __name__ == '__main__':
unittest.main()
|