使用unittest和ddt進行數據驅動
阿新 • • 發佈:2018-10-18
exception def eno pre 安裝 == error: mes 通過
1、安裝ddt
#pip install ddt
2、卸載ddt
# coding = utf-8 # encoding = utf-8 import ddt import time import unittest import logging import traceback from selenium import webdriver from selenium.common.exceptions import NoSuchElementException logging.basicConfig( level=logging.INFO, format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s‘, datefmt=‘%a %d %b %Y %H: %M: %S‘, filename=‘D://pytest//test//report.log‘, filemode=‘w‘ ) @ddt.ddt class TestDemo(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.maximize_window() @ddt.data([u"神奇動物在哪裏", u"葉茨"], [u"瘋狂動物城", u"古德溫"], [u"大話西遊之月光寶盒", u"周星馳"]) @ddt.unpack def test_dataDrivenByObj(self, testdata, expectdata): url = ‘http://www.baidu.com‘ self.driver.get(url) self.driver.implicitly_wait(10) try: self.driver.find_element_by_id("kw").send_keys(testdata) self.driver.find_element_by_id("su").click() time.sleep(3) self.assertTrue(expectdata in self.driver.page_source) except NoSuchElementException: logging.error(u"查找的頁面元素不存在:" + str(traceback.format_exc())) except AssertionError: logging.info(u"搜索:%s,期望:%s,失敗" % (testdata, expectdata)) else: logging.info(u"搜索:%s,期望:%s,通過" % (testdata, expectdata)) def tearDown(self): self.driver.quit() if __name__ == ‘__main__‘: unittest.main()
使用unittest和ddt進行數據驅動