这是错误
     self.assertFalse(ipValues[iNum],valueList2[iNum],"The result of sorti
TypeError: 'NoneType' object is unsubscriptable
源码: 文件名:logtest.py
#!/usr/bin/python                                                        
def countIP(file):
    ipCounter = {}
    testF = open(file,"r+")
    for line in testF.readlines():
        if '[client ' in line:
            ip = line.split('[client ')[1].split(']')[0]
            ipCounter[ip] = ipCounter.get(ip,0)+1
    ipCounter = sorted(ipCounter.iteritems(), key = lambda d:d[1], reverse=True)
    print (ipCounter)
    testF.close()
    return ipCounter
单测代码 test_logtest.py
import unittest
import logtest
class TestCase(unittest.TestCase):
def testother_file_test(self):
    """check mail_log in different text"""
    self.assertTrue(logtest.countIP("error_log.2015-11-18-18"))
def testvalue_test(self):
    """check ip`s value is sorted or not"""
    ipVList = {}
    ipValues = []
    ipVList = logtest.countIP("error_log.2015-11-18-18")
    for l in ipVList:
        ipValue = l[1]
        ipValues.append(ipValue)
    valueList2 = []
    valueList2 = ipValues.sort(reverse = True)                                                  
    if not ipValues:
        for iNum in range(len(ipValues)):
            self.assertFalse(ipValues[iNum],valueList2[iNum],"The result of sorting is wrong")
  if __name__ == '__main__':
        unittest.main()
这句错了
valueList2 = ipValues.sort(reverse = True)
list.sort返回的是None,
其实这个很好查。。错误说的是assert里面对None[idx],那么assert总共就2个[],分别是ipValues和valueList2。然后向上看两个变量的赋值。
ipValues = [],后面就一直是对这个列表变更。所以ipValues是好的。
而valueList2 = ipValues.sort(reverse = True) ,这一句那要看sort会返回什么值,这时不管查文档,还是在idle写两行代码测试,都能发现sort没返回值得。。
或者在assert前print这两个变量,也能发现问题
使用你的代码(我自己敲,调试找完了bug),然后用下面的log文件,测试正常:
[client 192.168.1.230]
[client 192.168.1.222]
结果:
$ python test.logtest.py 
192.168.1.230
192.168.1.222
[('192.168.1.230', 1), ('192.168.1.222', 1)]
.192.168.1.230
192.168.1.222
[('192.168.1.230', 1), ('192.168.1.222', 1)]
.
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
              我用的是python2.7。代码里本身不会出现没有 ipCounter 的问题,因为: