python读取mysql数据_Selenium(Python) ddt读取MySQL数据驱动
import unittest
from time import sleep
from ddt import ddt, data
from pymysql import connect
from selenium import webdriver
def getMySQLTestData():
# 查詢數(shù)據(jù)庫的方法
db = connect(host="localhost",
user="root",
password="123456",
db="world",
port=3306,
charset="utf8")
# 打開數(shù)據(jù)庫連接
cur = db.cursor()
# 使用cursor()方法獲取操作游標(biāo)
sql = "SELECT `search_word`, `search_result` FROM testdata;"
# sql語句
cur.execute(sql)
# 執(zhí)行sql語句
results = cur.fetchall()
# 獲取查詢的結(jié)果
db.commit()
# 提交
cur.close()
# 關(guān)閉游標(biāo)
db.close()
# 斷開數(shù)據(jù)庫連接
return results
# 返回一個list
@ddt
# ddt驅(qū)動
class MyTestCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.base_url = "https://www.baidu.com/"
self.driver.get(self.base_url)
self.driver.maximize_window()
sleep(2)
@data(*getMySQLTestData())
# 傳參
def test_something(self, sqlTestData):
searchTerm, searchResult = sqlTestData
driver = self.driver
driver.find_element_by_xpath(".//*[@id=‘kw‘]").send_keys(searchTerm)
driver.find_element_by_xpath(".//*[@id=‘su‘]").click()
sleep(2)
responseText = driver.find_element_by_xpath(".//*[@id=‘1‘]/h3/a").text
self.assertEqual(responseText, searchResult)
def tearDown(self):
self.driver.close()
self.driver.quit()
if __name__ == ‘__main__‘:
unittest.main()
總結(jié)
以上是生活随笔為你收集整理的python读取mysql数据_Selenium(Python) ddt读取MySQL数据驱动的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英特尔核显自定义分辨率_让免费的核显更好
- 下一篇: viewBox视图缩放(1)