標籤:data strong sleep mbr bsp 英文 expec file pytho
先在cmd環境 運行 pip install ddt 安裝資料驅動ddt模組
指令碼:
#encoding=utf-8
from selenium import webdriver
import unittest,time
import logging,traceback
import ddt
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:/report.log‘,
#開啟記錄檔的方式
filemode=‘w‘
)
@ddt.ddt#裝飾器
class TestDemo(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox(executable_path=‘c:\\geckodriver‘)
#資料驅動時指定的三個資料,每個資料是一個list
@ddt.data([u"神奇動物在哪裡",u"葉茨"],
[u"瘋狂動物城",u"古德溫"],
[u"大話西遊之月光寶盒",u"周星馳"])
@ddt.unpack#解包,將測試資料對應到testdata和expectdata,將上邊的list裡的兩個參數賦值給函數中,下邊有解包的例子
def test_dataDrivenByObj(self,testdata,expectdata):#傳參數
url="http://www.baidu.com"
#訪問百度首頁
self.driver.get(url)
#設定隱式等待時間為10秒,個別瀏覽器版的驅動可能會有問題,待驗證
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,e:
logging.error("element in page not existed, abnormal stack info:"+str(traceback.format_exc()))
except AssertionError,e:
logging.info("search ‘%s‘,expected ‘%s‘, failed" %(testdata,expectdata))
except Exception,e:
logging.error("unknown error, error message:"+str(traceback.format_exc()))
else:
logging.info(‘search "%s", expected "%s" passed‘ %(testdata,expectdata))
def tearDown(self):
self.driver.quit()
if __name__==‘__main__‘:
unittest.main()
結果:
如果日誌對象沒有問題的話,會把日誌打到檔案裡,如下(第一次運行時日誌對象中的filename錯寫成fimename了,就沒有產生report.log檔案,而是列印在了cmd裡)
亂碼是因為英文系統不支援中文導致的
D:\test_python>python task_test.py
Tue, 26 Jun 2018 12:48:12 task_test.py[line:54]INFO search "τ??σ??σè?τ??σ£?σ??Θ??", expected "σ?╢Φ??" passed
.Tue, 26 Jun 2018 12:48:28 task_test.py[line:54]INFO search "τ??τ?éσè?τ??σ??", expected "σ??σ╛╖μ╕?" passed
.Tue, 26 Jun 2018 12:48:43 task_test.py[line:54]INFO search "σ?oΦ?¥Φ?┐μ╕╕Σ╣?μ£êσà?σ?¥τ¢?", expected "σ??μ??Θ??" passed
.
----------------------------------------------------------------------
Ran 3 tests in 47.395s
OK
第二次運行時,修改了日誌對象,日誌就打在了report.log中了,控制台沒有打日誌
D:\test_python>python task_test.py
...
----------------------------------------------------------------------
Ran 3 tests in 46.251s
OK
Report.log:
內容:
Tue, 26 Jun 2018 12:49:41 task_test.py[line:54]INFO search "神奇動物在哪裡", expected "葉茨" passed
Tue, 26 Jun 2018 12:49:56 task_test.py[line:54]INFO search "瘋狂動物城", expected "古德溫" passed
Tue, 26 Jun 2018 12:50:12 task_test.py[line:54]INFO search "大話西遊之月光寶盒", expected "周星馳" passed
解包的例子:
>>> def add(a,b):
... return a+b
...
>>> add(1,2)
3
>>> add((1,2))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: add() takes exactly 2 arguments (1 given)
#直接傳元祖會報錯,但是前邊加個*就是解包的過程,把元素拆分出來,把元素分別賦值給add函數
>>> add(*(1,2))
3
python webdriver 測試架構-資料驅動DDT的例子