解决 TypeError: object of type ‘float‘ has no len() 问题 unittest单元测试框架 ddt data 数据驱动

这篇具有很好参考价值的文章主要介绍了解决 TypeError: object of type ‘float‘ has no len() 问题 unittest单元测试框架 ddt data 数据驱动。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1 问题

在unittest框架下,运用ddt和data模块进行数据驱动,脚本外存储数据时,报错。

TypeError: object of type 'float' has no len()

对象数据类型不够存储。

2 原因

excel文件中的数据单元格没有添加'英文的单引号,把数字当成文本来处理。电话号为11超出float数据类型的存储范围,文本就当成了字符串数据类型来处理。

3 办法

将单元格内添加'
例如下图:
解决 TypeError: object of type ‘float‘ has no len() 问题 unittest单元测试框架 ddt data 数据驱动,测试,python,单元测试,selenium,测试工具,chrome
附上在unittest框架下,运用ddt和data模块进行数据驱动,执行测试用例,以QQ注册页面为例子,代码如下:文章来源地址https://www.toymoban.com/news/detail-601367.html

# 导入自动化包
from selenium import webdriver
import time
# 导入单元测试框架
import unittest
from ddt import ddt,data
# 将excel数据转化为列表字典的方法导入
from excel_turnto_listdic import ExcelUtil

import warnings

# 创建类,继承TestCase类
@ddt
class E01(unittest.TestCase):
    '''测试用例的数据'''
    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中的数据以参数para形式传入测试用例
        # 创建浏览器对象
        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)
        # 点击【发送验证码】按钮
        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)
        # 点击“同意协议”
        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 数据驱动的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 解决报错TypeError: Object of type int32 is not JSON serializable

    当我们尝试将 numpy int32 对象转换为 JSON 字符串时,会出现 Python“TypeError: Object of type int32 is not JSON serializable”。 要解决该错误,请先将 numpy int 转换为 Python 整数,然后再将其转换为 JSON,例如 下面是错误如何发生的示例。 我们尝试将 numpy int32 对象传递给 json.dumps() 方法,但

    2024年02月06日
    浏览(45)
  • 【解决】MissingReferenceException: The object of type ‘GameObject‘ has been destroyed 观察者模式 监听物体被销毁

    MissingReferenceException: The object of type ‘Text’ has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. 该情况发生于我的 观察者模式 在 重新加载当前场景 时 监听的物体被 销毁 如上所示错误,通过分析,定位到错误是在观察

    2024年02月11日
    浏览(35)
  • python报‘AttributeError: type object ‘datetime.datetime‘ has no attribute ‘datetime‘‘错误的原因及解决方案

    在运行以下代码时出现报错AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’ 原因:在导入模块使用from datetime import datetime,由于包名和类名一样,导致系统识别出现混乱,无法知道哪个是具体包和类 解决方案:直接使用import datetime进行导包即可

    2024年02月13日
    浏览(33)
  • TypeError: ‘float‘ object is not subscriptable 已解决

    其实就是个小问题,但是爆出来的时候也很莫名其妙。因为之前都跑得好好的,只是换了不同的文件去跑才出的问题,关键是不同的文件要处理的内容和格式都是完全一样的,一个顺利跑完,一个就报TypeError: ‘float’ object is not subscriptable这个错,就非常无语。接下来就是看

    2024年02月11日
    浏览(34)
  • 【Python】成功解决TypeError: ‘float‘ object is not iterable

    【Python】成功解决TypeError: ‘float’ object is not iterable 🌈 个人主页:高斯小哥 🔥 高质量专栏:Matplotlib之旅:零基础精通数据可视化、Python基础【高质量合集】、PyTorch零基础入门教程👈 希望得到您的订阅和支持~ 💡 创作高质量博文(平均质量分92+),分享更多关于深度学习、

    2024年03月09日
    浏览(94)
  • 【趣解Bug】解决‘WebDriver‘ object has no attribute ‘find_element_by_xpath‘问题

    今天换了个电脑,之前在旧电脑里运行的自动化测试脚本突然失灵了,真的让人头秃,先看看问题 报错信息中显示WebDriver没有属性find_element_by_xpath,这怎么可能,这些代码在之前电脑里运行了千百遍,不可能说失效就失效的,悲伤持续了一秒钟,我就拿出百度大法,果然有解

    2023年04月10日
    浏览(27)
  • 解决selenium的WebDriver object has no attribute ‘find_element_by_xpath‘问题

    解决selenium的WebDriver object has no attribute \\\'find_element_by_xpath’问题 在使用selenium操作浏览器时,常见的一种定位元素的方式就是Xpath定位。但有时候会出现WebDriver object has no attribute \\\'find_element_by_xpath’的错误提示,意思是WebDriver对象没有该方法。 这通常是因为引入selenium包时未正

    2024年02月10日
    浏览(32)
  • 成功解决AttributeError: module ‘numpy‘ has no attribute ‘float‘.

    AttributeError: module ‘numpy’ has no attribute ‘float’. np.float was a deprecated alias for the builtin float . To avoid this error in existing code, use float by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use np.float64 here. The aliases was originally deprecated in NumPy 1.20; for

    2024年02月16日
    浏览(46)
  • Python之解决报错:TypeError: unsupported operand type(s) for /: ‘builtin_function_or_method‘ and ‘float‘

    开门见山哈家人们,首先解决方法:查找报错内容相关的代码,找到源头,添加小括号。 解释如下: 在运行Python程序的时候,报了个错,找了半天没找到什么原因。 因为给的报错信息在94行,反反复复盯着94行以及附近找了半个多小时,最后百度了半天发现问题出在了19行

    2024年02月15日
    浏览(28)
  • YOLOV5 | AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘ 问题解决 亲测有效

    如图: 1.点击报错行该路径,进入编辑页 2.将原代码(153-154行)修改为如下所示(155行): 即: 保存后再次运行detect.py,即可: 来看看训练结果吧 运行detect.py之前,应该将YOLOV5对应的权重文件放到项目文件夹下: 这里提供官方权重文件,可点击下方链接自行下载: YOLOv

    2024年02月13日
    浏览(85)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包