这个异常呢其实是因为我对list没有足够熟悉

我一开始很疑惑,明明已经正确返回testcase对象了呀,为啥会报TypeError: 'TestCase' object is not iterable这个错误 呢?

分析:

这个错误的意思是说TestCase这个对象是不可迭代的(注意到了吗,是TestCase,而不是testcase)

看print(testcase)输出的结果:

<TestCase.获取房源状态>

再看看是哪里调用了testcase对象,在runTest(testcase)函数中

使用for i in testcase 循环读出testcase对象当中的内容

首先testcase = runProject("all")会返回一个列表

我原本想是通过这句代码只取前2条数据,我以为这句代码执行后testcase = runProject("all")[2],testcase对象仍然是一个列表,但实际上这样写是只取了testcase列表中下标为2的元素,而不是testcase这个列表前2个元素了

正确的写法应该是这样的

testcase = runProject("all")[:2]

举个小例子说明一下:

for循环迭代一个列表--正常执行


In [2]: a=["a","b",123]                                                       

In [4]: for i in a:
...: print(i)
...:
a
b
123

for循环迭代a[:2](a[:2]仍然是一个列表对象)--正常执行

In [8]: a[:2]
Out[8]: ['a', 'b']

In [6]: for i in a[:2]:
...: print(i)
...:
a
b

for循环迭代a[2](a[2]表示列表中下标为2的元素,该元素是数字123,该对象是不可迭代的)--报错了TypeError: 'int' object is not iterable

In [9]: a[2]
Out[9]: 123

In [7]: for i in a[2]:
...: print(i)
...:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-879d94b1fc77> in <module>
----> 1 for i in a[2]:
2 print(i)
3 TypeError: 'int' object is not iterable

顺例复习一下:

python中可以迭代的对象包括:字符串,列表,元组,字典,文件

TypeError: 'TestCase' object is not iterable的更多相关文章

  1. TypeError: 'ExcelData' object is not iterable

    今天写了个测试的代码,结果在执行test_register.py文件在调用readexcle.py的时候一直报错TypeError: 'ExcelData' object is not iterabl ...

  2. python TypeError: 'NoneType' object is not iterable

    list(set(map(lambda tp_id : tp_id if not ('#' in tp_id) and len(tp_id.strip().replace('\n', '')) > ...

  3. python"TypeError: 'NoneType' object is not iterable"错误解析

    尊重原创博主,原文链接:https://blog.csdn.net/dataspark/article/details/9953225 [解析] 一般是函数返回值为None,并被赋给了多个变量. 实例 ...

  4. Python : TypeError: 'int' object is not iterable

    用循环依次对list中的每个名字打印出 Hello, xxx! -------------------------------------------------------- L = ['Bart' ...

  5. append导致TypeError: 'NoneType' object is not iterable

    a=[1,2,3] a.append(4) a Out[4]: [1, 2, 3, 4] a=a.append(5) print(a) None a =[1,2,3] print(a.append(4 ...

  6. python框架Scrapy报错TypeError: 'float' object is not iterable解决

    原因是:Twisted版本高了. 解决办法: 只要把Twisted库降级到16.6.0即可: pip3 install Twisted== 注:Twisted16..0安装后,会自动卸载高版本的Twi ...

  7. selenium报错TypeError: 'FirefoxWebElement' object is not iterable

    报错原因element少了s定位一组元素的方法与定位单个元素的方法类似,唯一的区别是在单词element后面多了一个s表示复数. 改为 返回结果为

  8. 'NoneType' object is not iterable

    "TypeError: 'NoneType' object is not iterable" 一般是返回值为None同时赋值给了多个变量

  9. Python问题:'Nonetype' object is not iterable

    参考链接:http://blog.csdn.net/dataspark/article/details/9953225 [解析] 这个错误提示一般发生在将None赋给多个值时. [案例] 定义了如下的 ...

随机推荐

  1. 省市联动选择的一个demo,利用vue+webpack+amaze-vue实现省市区联动选择组件

    https://github.com/sunshineJi/vue-city-picker

  2. HDOJ 5387 Clock 水+模拟

    Clock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Subm ...

  3. SSH-struts2的异常处理

    在学习j2se的时候学习过利用java的exception类去处理异常.在struts2框架中也提供了对于异常的处理.简单说就是当Action发生异常时.能够在struts2.xml文件里进行配置,将 ...

  4. java中的ShortBuffer

    一.概述 java.lang.Object java.nio.Buffer java.nio.ShortBuffer public abstract class ShortBuffer extends ...

  5. (七)Java 变量类型

    Java 变量类型 在Java语言中,所有的变量在使用前必须声明.声明变量的基本格式如下: type identifier [ = value][, identifier [= value] ...] ...

  6. kendo datepicker汉化

    kendo grid 支持多语言,包括的语言有非常多种.一般默认情况是使用en,可是对于国内市场的话我们须要使用汉字.不墨迹了.     <link href="http://cdn. ...

  7. sql server 生成随机数 rand函数

    https://docs.microsoft.com/en-us/sql/t-sql/functions/rand-transact-sql?view=sql-server-2017 在某一个区间内生 ...

  8. JfreeChart的使用1

    JfreeChart的使用 来自: 克洛泽大地(DREG) 2008-07-03 14:35:11 先从网上找点介绍. 一.简介 WW 的发展使得基于因特网的应用程序不再局限于静态或者简单的动态内容提 ...

  9. 【Codevs 3115】高精度练习之减法

    http://codevs.cn/problem/3115/ 板子题~ // <H.cpp> - Sun Oct 9 12:58:23 2016 // This file is made ...

  10. java笔记线程电影院卖票最终版

    * 如何解决线程安全问题呢? *  * 要想解决问题,就要知道哪些原因会导致出问题:(而且这些原因也是以后我们判断一个程序是否会有线程安全问题的标准) * A:是否是多线程环境 * B:是否有共享数据 ...