1 問題
在unittest框架下,運用ddt和data模塊進行數(shù)據(jù)驅動,腳本外存儲數(shù)據(jù)時,報錯。
TypeError: object of type 'float' has no len()
對象數(shù)據(jù)類型不夠存儲。
2 原因
excel文件中的數(shù)據(jù)單元格沒有添加'
英文的單引號,把數(shù)字當成文本來處理。電話號為11超出float數(shù)據(jù)類型的存儲范圍,文本就當成了字符串數(shù)據(jù)類型來處理。文章來源:http://www.zghlxwxcb.cn/news/detail-601367.html
3 辦法
將單元格內添加'
例如下圖:
附上在unittest框架下,運用ddt和data模塊進行數(shù)據(jù)驅動,執(zhí)行測試用例,以QQ注冊頁面為例子,代碼如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-601367.html
# 導入自動化包
from selenium import webdriver
import time
# 導入單元測試框架
import unittest
from ddt import ddt,data
# 將excel數(shù)據(jù)轉化為列表字典的方法導入
from excel_turnto_listdic import ExcelUtil
import warnings
# 創(chuàng)建類,繼承TestCase類
@ddt
class E01(unittest.TestCase):
'''測試用例的數(shù)據(jù)'''
obj1 = ExcelUtil(r"./Data/abc.xlsx")
dicData = obj1.dic_data()
# setUpClass方法需要用修飾器
@classmethod
def setUpClass(self):
warnings.simplefilter('ignore', ResourceWarning)
def setUp(self) -> None:
pass
#第一條測試用例
@data(*dicData)
def test_t_1(self,para): # data中的數(shù)據(jù)以參數(shù)para形式傳入測試用例
# 創(chuàng)建瀏覽器對象
driver = webdriver.Chrome()
# 打開網址
driver.get("https://ssl.zc.qq.com/v3/index-chs.html")
time.sleep(2)
# 在昵稱文本框輸入合法字符
driver.find_element_by_id("nickname").send_keys(para["username"])
time.sleep(2)
# 輸入有效密碼
driver.find_element_by_id("password").send_keys(para["password"])
time.sleep(2)
# 輸入有效手機號碼
driver.find_element_by_id("phone").send_keys(para["phonenumber"])
time.sleep(2)
# 點擊【發(fā)送驗證碼】按鈕
driver.find_element_by_id("send-sms").click()
time.sleep(2)
# 輸入驗證碼
driver.find_element_by_xpath('//*[@id="code"]').send_keys(para["vcode"])
time.sleep(2)
# 點擊“同意協(xié)議”
driver.find_element_by_xpath('/html/body/div[3]/div[2]/div[1]/form/div[8]/label/img[2]').click()
time.sleep(2)
# 點擊【立即注冊】按鈕
driver.find_element_by_id("get_acc").click()
time.sleep(2)
driver.quit()
def tearDown(self) -> None:
pass
@classmethod
def tearDownClass(cls) -> None:
pass
if __name__ == '__main__':
unittest.main()
# path = os.path.dirname(__file__)
# print(path)
# site01 = unittest.defaultTestLoader.discover(path, pattern="test_t_1.py")
# with open(r'./re.txt','wb') as f:
# runner = unittest.TextTestRunner(f,descriptions="測試報告",verbosity=2)
# runner.run(site01)
到了這里,關于解決 TypeError: object of type ‘float‘ has no len() 問題 unittest單元測試框架 ddt data 數(shù)據(jù)驅動的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!